##################################################################
##################################################################
## Replication Material
## Tobias Widmann:  Do Politicians Appeal to Discrete Emotions? The Effect of Wind Turbine Construction on Elite Discourse 
## Journal of Politics
## widmann@ps.au.dk
##
## Script 003: Replication of the Appendix
##################################################################
##################################################################

# Note: The file 000_README.pdf describes all scripts and datasets required to replicate the analysis

# This script was run on the following R version, platform and OS:
# R version 4.0.5 (2021-03-31)
# Platform: x86_64-apple-darwin17.0 (64-bit)
# Running under: macOS Big Sur 11.5.1


#### Set Working Directory to the Replication Folder# ###########################

# Set the directory to the replication folder
setwd("./files")

#### Install & Load Packages ##############################################################

sessionInfo()

install.packages("devtools", version = "2.4.0")
devtools::install_version("tidyverse", version = "1.3.1")
devtools::install_version("dplyr", version = "1.0.5")
devtools::install_version("openxlsx", version = "4.2.3")
devtools::install_version("tidytext", version = "0.3.1")
devtools::install_version("quanteda", version = "3.0.0")
devtools::install_version("plm", version = "2.4-1")
devtools::install_version("lmtest", version = "0.9-40")
devtools::install_version("dotwhisker", version = "0.7.4")
devtools::install_version("modelsummary", version = "1.4.2")
devtools::install_version("data.table", version = "1.14.0")
devtools::install_version("fixest", version = "0.11.1")
devtools::install_version("pbkrtest", version = "0.5.1")
devtools::install_version("car", version = "3.1-2")
devtools::install_version("rstatix", version = "0.7.2")

library(tidyverse)
library(dplyr)
library(tidytext)
library(quanteda)
library(plm)
library(lmtest)
library(dotwhisker)
library(pbkrtest)
library(car)
library(rstatix)
library(ggpubr)
library(modelsummary)
library(data.table)
library(fixest)

#### Load Data ########################################################################
load("./parl_speech.Rdata")
load("./wind_data.Rdata")
load("./afd_tweets.Rdata")
load("./linke_tweets.Rdata")
load("./spd_tweets.Rdata")
load("./sz_tweets.Rdata")
load("./wind_tweets.Rdata")
load("./wind_data_approval.Rdata")


#### Figure A1 ########################################################################

# Group the 'ws_parl' data frame by 'treat_indirect' and 'party' columns.
# Summarize the data to compute mean, standard deviation, and count for the 'topic56' column.
# Then, calculate the standard error and the confidence intervals for the mean of 'topic56'.
df56<- ws_parl %>%
  group_by(treat_indirect, party) %>%
  dplyr::summarize(t_prop = mean(topic56, na.rm = T),
                   sd_tp = sd(topic56, na.rm = TRUE),
                   n_tp = n()) %>%
  mutate(se_tp = sd_tp / sqrt(n_tp),
         lower.ci = t_prop - qt(1 - (0.05 / 2), n_tp - 1) * se_tp,
         upper.ci = t_prop + qt(1 - (0.05 / 2), n_tp - 1) * se_tp)

# Plot the mean 'topic56' proportion for each group ('treat_indirect' and 'party') with confidence intervals.
# The data is facetted by 'party'.
df56 %>%
  ggplot(aes(t_prop, as.factor(treat_indirect), group = party)) +
  geom_point(color = "#CC6666") +
  geom_errorbarh(aes(xmin = lower.ci, xmax = upper.ci), height = .1, color = "#CC6666") +
  theme_bw() +
  facet_wrap(~fct_rev(party)) +
  labs(x = "Topic Proportion (Topic 56)", y = "Treatment")
       

# Exporting figure to .pdf
ggsave("./figures/figureA1.pdf")

#### Figure A2 ##############################################################################

# Create a new column 'topic_combined' in 'ws_parl' data frame by adding values from 'topic16' and 'topic56' columns.
ws_parl$topic_combined <- ws_parl$topic16 + ws_parl$topic56

# Group the 'ws_parl' data frame by 'treat_indirect' column and compute summary statistics for 'topic16'.
# Calculate mean, standard deviation, and count for 'topic16' for each group.
# Then, compute the standard error and 95% confidence intervals for the mean of 'topic16'.
df16 <- ws_parl %>%
  group_by(treat_indirect) %>%
  dplyr::summarize(t_prop = mean(topic16, na.rm = T),
                   sd_tp = sd(topic16, na.rm = TRUE),
                   n_tp = n()) %>%
  mutate(se_tp = sd_tp / sqrt(n_tp),
         lower.ci = t_prop - qt(1 - (0.05 / 2), n_tp - 1) * se_tp,
         upper.ci = t_prop + qt(1 - (0.05 / 2), n_tp - 1) * se_tp)

# Plot the mean 'topic16' proportion for each 'treat_indirect' group with 95% confidence intervals.
plot16 <- df16 %>%
  ggplot(aes(t_prop, as.factor(treat_indirect))) +
  geom_point(color = "#CC6666") +
  geom_errorbarh(aes(xmin = lower.ci, xmax = upper.ci), height = .1, color = "#CC6666") +
  theme_bw() +
  labs(x = "Topic Proportion (Topic 16)", y = "Treatment")


# Group the 'ws_parl' data frame by 'treat_indirect' column and compute summary statistics for 'topic_combined'.
# Calculate mean, standard deviation, and count for 'topic_combined' for each group.
# Then, compute the standard error and 95% confidence intervals for the mean of 'topic_combined'.
df_combined <- ws_parl %>%
  group_by(treat_indirect) %>%
  dplyr::summarize(t_prop = mean(topic_combined, na.rm = T),
                   sd_tp = sd(topic_combined, na.rm = TRUE),
                   n_tp = n()) %>%
  mutate(se_tp = sd_tp / sqrt(n_tp),
         lower.ci = t_prop - qt(1 - (0.05 / 2), n_tp - 1) * se_tp,
         upper.ci = t_prop + qt(1 - (0.05 / 2), n_tp - 1) * se_tp)

# Plot the mean 'topic_combined' proportion for each 'treat_indirect' group with 95% confidence intervals.
plot_combined <- df_combined %>%
  ggplot(aes(t_prop, as.factor(treat_indirect))) +
  geom_point(color = "#CC6666") +
  geom_errorbarh(aes(xmin = lower.ci, xmax = upper.ci), height = .1, color = "#CC6666") +
  theme_bw() +
  labs(x = "Topic Proportion (Topic 16 + 56 combined)", y = "Treatment")

# Arrange the two plots (for 'topic16' and 'topic_combined') side by side.
ggarrange(plot16, plot_combined,
          ncol = 2, nrow = 1)

# Exporting figure to .pdf
ggsave("./figures/figureA2.pdf")

#### Figure A3 #############################################################################

# Group the 'ws_parl' data frame by 'party' column.
# Calculate mean, standard deviation, and count for 'topic16' for each party.
# Then, compute the standard error and 95% confidence intervals for the mean of 'topic16'.
df16_party <- ws_parl %>%
  group_by(party) %>%
  dplyr::summarize(t_prop = mean(topic16, na.rm = T),
                   sd_tp = sd(topic16, na.rm = TRUE),
                   n_tp = n()) %>%
  mutate(se_tp = sd_tp / sqrt(n_tp),
         lower.ci = t_prop - qt(1 - (0.05 / 2), n_tp - 1) * se_tp,
         upper.ci = t_prop + qt(1 - (0.05 / 2), n_tp - 1) * se_tp)

# Plot the mean 'topic16' proportion for each party with 95% confidence intervals.
plot16_party <- df16_party %>%
  ggplot(aes(t_prop, as.factor(party))) +
  geom_point(color = "#CC6666") +
  geom_errorbarh(aes(xmin = lower.ci, xmax = upper.ci), height = .1, color = "#CC6666") +
  theme_bw() +
  labs(x = "Topic Proportion (Topic 16)", y = "Party")

# Group the 'ws_parl' data frame by 'party' column.
# Calculate mean, standard deviation, and count for 'topic_combined' for each party.
# Then, compute the standard error and 95% confidence intervals for the mean of 'topic_combined'.
df_combined_party <- ws_parl %>%
  group_by(party) %>%
  dplyr::summarize(t_prop = mean(topic_combined, na.rm = T),
                   sd_tp = sd(topic_combined, na.rm = TRUE),
                   n_tp = n()) %>%
  mutate(se_tp = sd_tp / sqrt(n_tp),
         lower.ci = t_prop - qt(1 - (0.05 / 2), n_tp - 1) * se_tp,
         upper.ci = t_prop + qt(1 - (0.05 / 2), n_tp - 1) * se_tp)

# Plot the mean 'topic_combined' proportion for each party with 95% confidence intervals.
plot_combined_party <- df_combined_party %>%
  ggplot(aes(t_prop, as.factor(party))) +
  geom_point(color = "#CC6666") +
  geom_errorbarh(aes(xmin = lower.ci, xmax = upper.ci), height = .1, color = "#CC6666") +
  theme_bw() +
  labs(x = "Topic Proportion (Topic 16 + 56 combined)", y = "Party")

# Arrange the two plots (for 'topic16' and 'topic_combined') side by side.
ggarrange(plot16_party, plot_combined_party,
          ncol = 2, nrow = 1)


# Exporting figure to .pdf
ggsave("./figures/figureA3.pdf")

#### Figure A4 #############################################################################

# Group the 'ws_parl' data frame by 'treat_indirect' and 'party' columns.
df16<- ws_parl %>%
  group_by(treat_indirect, party) %>%
  dplyr::summarize(t_prop = mean(topic16, na.rm = T),
                   sd_tp = sd(topic16, na.rm = TRUE),
                   n_tp = n()) %>%
  mutate(se_tp = sd_tp / sqrt(n_tp),
         lower.ci = t_prop - qt(1 - (0.05 / 2), n_tp - 1) * se_tp,
         upper.ci = t_prop + qt(1 - (0.05 / 2), n_tp - 1) * se_tp)

# Plot the mean 'topic16' proportion for each 'treat_indirect' level, split by 'party'.
df16 %>%
  ggplot(aes(t_prop, as.factor(treat_indirect), group = party)) +
  geom_point(color = "#CC6666") +
  geom_errorbarh(aes(xmin = lower.ci, xmax = upper.ci), height = .1, color = "#CC6666") +
  theme_bw() +
  facet_wrap(~fct_rev(party)) +
  labs(x = "Topic Proportion (Topic 16)", y = "Treatment")


# Exporting figure to .pdf
ggsave("./figures/figureA4.pdf")

#### Figure A5 #############################################################################


df_combined <- ws_parl %>%
  group_by(treat_indirect, party) %>%
  dplyr::summarize(t_prop = mean(topic_combined, na.rm = T),
                   sd_tp = sd(topic_combined, na.rm = TRUE),
                   n_tp = n()) %>%
  mutate(se_tp = sd_tp / sqrt(n_tp),
         lower.ci = t_prop - qt(1 - (0.05 / 2), n_tp - 1) * se_tp,
         upper.ci = t_prop + qt(1 - (0.05 / 2), n_tp - 1) * se_tp)

df_combined %>%
  ggplot(aes(t_prop, as.factor(treat_indirect), group = party)) +
  geom_point(color = "#CC6666") +
  geom_errorbarh(aes(xmin = lower.ci, xmax = upper.ci), height = .1, color = "#CC6666") +
  theme_bw() +
  facet_wrap(~fct_rev(party)) +
  labs(x = "Topic Proportion (Topic 16 + 56)", y = "Treatment")

# Exporting figure to .pdf
ggsave("./figures/figureA5.pdf")

#### Table B1 & B2 ######################################################################################

# Start by assigning parl_speeches data to a new dataframe for analysis.
df_analysis <- ws_parl

# Convert the 'date2' column to numeric and subtract 17434.
df_analysis$date3 <- as.numeric(df_analysis$date2) - 17434

# Initialize counters k and j.
k <- 1
j <- 0

# Create a new 'months' column in df_analysis, setting it to NULL initially.
df_analysis$months <- NULL

# Loop through a sequence of numbers to assign a month value based on the 'date3' column.
for (i in seq(from=30, to=1200, by=30)){
  df_analysis$months[df_analysis$date3 < i & df_analysis$date3 > j] <- k
  j <- i
  k <- k + 1
}

# Filter df_analysis to remove rows where 'wkindirekt' is NA.
df_analysis2 <- df_analysis[!is.na(df_analysis$wkindirekt),]

# Filter df_analysis2 to keep rows where 'topic' is either 56 or 16.
df3 <- df_analysis2[df_analysis2$topic == 56 | df_analysis2$topic == 16,]

# Remove spaces from the 'party' column and also remove any trailing whitespace.
df_analysis2$party <- gsub(" ", "", df_analysis2$party)
df_analysis2$party <- gsub("(*UCP)\\s*", "", df_analysis2$party, perl = TRUE)

# Create subsets of df3 for each party, filtering based on the 'treat_indirect' column.
afd <- df3[!(df3$party != "AfD" & df3$treat_indirect == 1),]
green <- df3[!(df3$party != "Greens" & df3$treat_indirect == 1),]
spd <- df3[!(df3$party != "SPD" & df3$treat_indirect == 1),]
linke <- df3[!(df3$party != "The Left" & df3$treat_indirect == 1),]
cdu <- df3[!(df3$party != "CDU/CSU" & df3$treat_indirect == 1),]
fdp <- df3[!(df3$party != "FDP" & df3$treat_indirect == 1),]

# The following blocks of code aggregate the data for each party and a specific emotion ('el.anger' or 'el.disgust'), 
# A panel data model (fixed effects model) is then estimated using the 'plm' package. 
# The results of the model are extracted and stored in a dataframe.

# Analysis for the Greens party and 'el.anger'.
df_agg <- aggregate(green$el.anger, list(green$name, green$months, green$count, green$party), mean)
# Rename columns for clarity.
colnames(df_agg) <- c("name", "date", "treat_indirect", "party", "anger")

# Sort data and keep distinct rows.
df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect)), ]
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

# Estimate the fixed effects model.
fixed1 <- plm(anger ~ treat_indirect, model="within", index = c("name", "date"), 
              effect = "twoways", data = df_agg)

# Robust standard errors are calculated using heteroskedasticity-consistent (HC0) covariance matrix estimation. Clustering is applied on the 'group' variable to account for potential correlations within groups.
model1 <- coeftest(fixed1, vcov=vcovHC(fixed1, type="HC0", cluster="group"), save = TRUE)

# Extract model results.
dfdf1 <- broom::tidy(model1)
dfdf1$model <- "Anger"
dfdf1$term <- "Greens"

#Repeat for Disgust and other parties
df_agg <- aggregate(green$el.disgust, list(green$name, green$months, green$count, green$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "disgust"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$disgust2 <- scale(df_agg$disgust)

fixed2 <- plm(disgust ~ treat_indirect, model="within", index = c("name", "date"), 
              effect = "twoways", data = df_agg)
model2 <- coeftest(fixed2, vcov=vcovHC(fixed2,type="HC0",cluster="group"), save = TRUE)

dfdf2 <- broom::tidy(model2)
dfdf2$model <- "Disgust"
dfdf2$term <- "Greens"


df_agg <- aggregate(afd$el.anger, list(afd$name, afd$months, afd$count, afd$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "anger"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)



fixed3 <- plm(anger ~ treat_indirect, model="within", index = c("name", "date"), 
              effect = "twoways", data = df_agg)
model3 <- coeftest(fixed3, vcov=vcovHC(fixed3,type="HC0",cluster="group"), save = TRUE)

dfdf3 <- broom::tidy(model3)
dfdf3$model <- "Anger"
dfdf3$term <- "AfD"

df_agg <- aggregate(afd$el.disgust, list(afd$name, afd$months, afd$count, afd$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "disgust"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$disgust2 <- scale(df_agg$disgust)

fixed4 <- plm(disgust ~ treat_indirect, model="within", index = c("name", "date"), 
              effect = "twoways", data = df_agg)
model4 <- coeftest(fixed4, vcov=vcovHC(fixed4,type="HC0",cluster="group"), save = TRUE)

dfdf4 <- broom::tidy(model4)
dfdf4$model <- "Disgust"
dfdf4$term <- "AfD"

df_agg <- aggregate(cdu$el.anger, list(cdu$name, cdu$months, cdu$count, cdu$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "anger"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)



fixed5 <- plm(anger ~ treat_indirect, model="within", index = c("name", "date"), 
              effect = "twoways", data = df_agg)
model5 <- coeftest(fixed5, vcov=vcovHC(fixed5,type="HC0",cluster="group"), save = TRUE)

dfdf5 <- broom::tidy(model5)
dfdf5$model <- "Anger"
dfdf5$term <- "CDU/CSU"

df_agg <- aggregate(cdu$el.disgust, list(cdu$name, cdu$months, cdu$count, cdu$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "disgust"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$disgust2 <- scale(df_agg$disgust)

fixed6 <- plm(disgust ~ treat_indirect, model="within", index = c("name", "date"), 
              effect = "twoways", data = df_agg)
model6 <- coeftest(fixed6, vcov=vcovHC(fixed6,type="HC0",cluster="group"), save = TRUE)

dfdf6 <- broom::tidy(model6)
dfdf6$model <- "Disgust"
dfdf6$term <- "CDU/CSU"



df_agg <- aggregate(fdp$el.anger, list(fdp$name, fdp$months, fdp$count, fdp$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "anger"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)



fixed7 <- plm(anger ~ treat_indirect, model="within", index = c("name", "date"), 
              effect = "twoways", data = df_agg)
model7 <- coeftest(fixed7, vcov=vcovHC(fixed7,type="HC0",cluster="group"), save = TRUE)

dfdf7 <- broom::tidy(model7)
dfdf7$model <- "Anger"
dfdf7$term <- "FDP"

df_agg <- aggregate(fdp$el.disgust, list(fdp$name, fdp$months, fdp$count, fdp$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "disgust"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$disgust2 <- scale(df_agg$disgust)

fixed8 <- plm(disgust ~ treat_indirect, model="within", index = c("name", "date"), 
              effect = "twoways", data = df_agg)
model8 <- coeftest(fixed8, vcov=vcovHC(fixed8,type="HC0",cluster="group"), save = TRUE)

dfdf8 <- broom::tidy(model8)
dfdf8$model <- "Disgust"
dfdf8$term <- "FDP"

df_agg <- aggregate(spd$el.anger, list(spd$name, spd$months, spd$count, spd$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "anger"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)



fixed9 <- plm(anger ~ treat_indirect, model="within", index = c("name", "date"), 
              effect = "twoways", data = df_agg)
model9 <- coeftest(fixed9, vcov=vcovHC(fixed9,type="HC0",cluster="group"), save = TRUE)

dfdf9 <- broom::tidy(model9)
dfdf9$model <- "Anger"
dfdf9$term <- "SPD"

df_agg <- aggregate(spd$el.disgust, list(spd$name, spd$months, spd$count, spd$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "disgust"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$disgust2 <- scale(df_agg$disgust)

fixed10 <- plm(disgust ~ treat_indirect, model="within", index = c("name", "date"), 
               effect = "twoways", data = df_agg)
model10 <- coeftest(fixed10, vcov=vcovHC(fixed10,type="HC0",cluster="group"), save = TRUE)

dfdf10 <- broom::tidy(model10)
dfdf10$model <- "Disgust"
dfdf10$term <- "SPD"


df_agg <- aggregate(linke$el.anger, list(linke$name, linke$months, linke$count, linke$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "anger"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)



fixed11 <- plm(anger ~ treat_indirect, model="within", index = c("name", "date"), 
               effect = "twoways", data = df_agg)
model11 <- coeftest(fixed11, vcov=vcovHC(fixed11,type="HC0",cluster="group"), save = TRUE)

dfdf11 <- broom::tidy(model11)
dfdf11$model <- "Anger"
dfdf11$term <- "The Left"

df_agg <- aggregate(linke$el.disgust, list(linke$name, linke$months, linke$count, linke$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "disgust"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$disgust2 <- scale(df_agg$disgust)

fixed12 <- plm(disgust ~ treat_indirect, model="within", index = c("name", "date"), 
               effect = "twoways", data = df_agg)
model12 <- coeftest(fixed12, vcov=vcovHC(fixed12,type="HC0",cluster="group"), save = TRUE)

dfdf12 <- broom::tidy(model12)
dfdf12$model <- "Disgust"
dfdf12$term <- "The Left"

# Table B1
models <- list("Greens" = model1, "AfD" = model3, "CDU/CSU" = model5, "FDP" = model7, "SPD" = model9, "The Left" = model11)
modelsummary(models, coef_rename = c("treat_indirect" = "Additional wind turbine"), stars = TRUE, title = 'Anger', 
             output = "./tables/tableB1.html")

# Table B2
models <- list("Greens" = model2, "AfD" = model4, "CDU/CSU" = model6, "FDP" = model8, "SPD" = model10, "The Left" = model12)
modelsummary(models, coef_rename = c("treat_indirect" = "Additional wind turbine"), stars = TRUE, title = 'Disgust', 
             output = "./tables/tableB2.html")

#### Figure C1 #############################################################################
# Copy the ws_parl dataframe to df_analysis for further manipulation.
df_analysis <- ws_parl

# Convert date2 to numeric and subtract a constant (17434) to create a new column date3.
df_analysis$date3 <- as.numeric(df_analysis$date2) - 17434

# Initialize counters.
k <- 1
j <- 0

# Initialize a new column months with NULL values.
df_analysis$months <- NULL

# Break the date3 column into 40 intervals (or 'months') of 30 units each.
for (i in seq(from=30, to=1200, by=30)){
  df_analysis$months[df_analysis$date3 < i & df_analysis$date3 > j] <- k
  j <- i
  k <- k + 1
}

# Filter rows where wkindirekt is not NA.
df_analysis2 <- df_analysis[!is.na(df_analysis$wkindirekt),]

# Filter rows where wkdirekt is not 0.
df_analysis3 <- df_analysis[df_analysis$wkdirekt != 0,]

# From df_analysis2, retain only rows related to topics 56 and 16.
df3 <- df_analysis2[df_analysis2$topic == 56 | df_analysis2$topic == 16,]

# Clean the party names by removing spaces and Unicode character property escapes.
df_analysis2$party <- gsub(" ", "", df_analysis2$party)
df_analysis2$party <- gsub("(*UCP)\\s*", "", df_analysis2$party, perl = TRUE)

# Create party-specific dataframes filtering by party and retaining rows where treat_indirect is 1.
afd <- df3[!(df3$party != "AfD" & df3$treat_indirect == 1),]
green <- df3[!(df3$party != "Greens" & df3$treat_indirect == 1),]
spd <- df3[!(df3$party != "SPD" & df3$treat_indirect == 1),]
linke <- df3[!(df3$party != "The Left" & df3$treat_indirect == 1),]
cdu <- df3[!(df3$party != "CDU/CSU" & df3$treat_indirect == 1),]
fdp <- df3[!(df3$party != "FDP" & df3$treat_indirect == 1),]

# The purpose of df_fesa is not clear as it's set to NULL and not used afterward.
df_fesa <- NULL

# Aggregate the fear scores (el.fear) for the party linke based on name, months, count, and party.
# Calculate the mean fear score for each group.
df_agg <- aggregate(linke$el.fear, list(linke$name, linke$months, linke$count, linke$party), mean)

# Rename columns for clarity.
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "fear"

# Sort the dataframe by name and treat_indirect.
df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect)),]

# Remove duplicate rows based on name and date.
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

# Scale the fear scores to create a new column fear2.
df_agg$fear2 <- scale(df_agg$fear)

# Perform a fixed effects regression analysis with fear as the dependent variable and treat_indirect as the independent variable.
# Account for both individual (name) and time (date) effects.
fixed1 <- plm(fear ~ treat_indirect, model = "within", index = c("name", "date"), 
              effect = "twoways", data = df_agg)

# Adjust the standard errors of the coefficients for heteroskedasticity and autocorrelation.
model1 <- coeftest(fixed1, vcov = vcovHC(fixed1, type = "HC0", cluster = "group"))

# Extract the results of the regression analysis into a tidy format.
dfdf1 <- broom::tidy(model1)

# Label the model and term for clarity.
dfdf1$model <- "fear"
dfdf1$term <- "The Left"

# Repeat for sadness and other parties
df_agg <- aggregate(linke$el.sadness, list(linke$name, linke$months, linke$count, linke$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "sadness"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$sadness2 <- scale(df_agg$sadness)


fixed2 <- plm(sadness ~ treat_indirect, model="within", index = c("name", "date"), 
              effect = "twoways", data = df_agg)
model2 <- coeftest(fixed2, vcov=vcovHC(fixed2,type="HC0",cluster="group"))

dfdf2 <- broom::tidy(model2)
dfdf2$model <- "sadness"
dfdf2$term <- "The Left"

df_fesa <- rbind(dfdf1, dfdf2, df_fesa)


df_agg <- aggregate(green$el.fear, list(green$name, green$months, green$count, green$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "fear"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$fear2 <- scale(df_agg$fear)


fixed3 <- plm(fear ~ treat_indirect, model="within", index = c("name", "date"), 
              effect = "twoways", data = df_agg)
model3 <- coeftest(fixed3, vcov=vcovHC(fixed3,type="HC0",cluster="group"))


dfdf1 <- broom::tidy(model3)
dfdf1$model <- "fear"
dfdf1$term <- "Greens"


df_agg <- aggregate(green$el.sadness, list(green$name, green$months, green$count, green$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "sadness"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$sadness2 <- scale(df_agg$sadness)


fixed4 <- plm(sadness ~ treat_indirect, model="within", index = c("name", "date"), 
              effect = "twoways", data = df_agg)
model4 <- coeftest(fixed4, vcov=vcovHC(fixed4,type="HC0",cluster="group"))

dfdf2 <- broom::tidy(model4)
dfdf2$model <- "sadness"
dfdf2$term <- "Greens"

df_fesa <- rbind(dfdf1, dfdf2, df_fesa)


df_agg <- aggregate(spd$el.fear, list(spd$name, spd$months, spd$count, spd$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "fear"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$fear2 <- scale(df_agg$fear)


fixed5 <- plm(fear ~ treat_indirect, model="within", index = c("name", "date"), 
              effect = "twoways", data = df_agg)
model5 <- coeftest(fixed5, vcov=vcovHC(fixed5,type="HC0",cluster="group"))


dfdf1 <- broom::tidy(model5)
dfdf1$model <- "fear"
dfdf1$term <- "SPD"


df_agg <- aggregate(spd$el.sadness, list(spd$name, spd$months, spd$count, spd$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "sadness"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$sadness2 <- scale(df_agg$sadness)


fixed6 <- plm(sadness ~ treat_indirect, model="within", index = c("name", "date"), 
              effect = "twoways", data = df_agg)
model6 <- coeftest(fixed6, vcov=vcovHC(fixed6,type="HC0",cluster="group"))

dfdf2 <- broom::tidy(model6)
dfdf2$model <- "sadness"
dfdf2$term <- "SPD"

df_fesa <- rbind(dfdf1, dfdf2, df_fesa)



df_agg <- aggregate(cdu$el.fear, list(cdu$name, cdu$months, cdu$count, cdu$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "fear"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$fear2 <- scale(df_agg$fear)


fixed7 <- plm(fear ~ treat_indirect, model="within", index = c("name", "date"), 
              effect = "twoways", data = df_agg)
model7 <- coeftest(fixed7, vcov=vcovHC(fixed7,type="HC0",cluster="group"))


dfdf1 <- broom::tidy(model7)
dfdf1$model <- "fear"
dfdf1$term <- "CDU/CSU"


df_agg <- aggregate(cdu$el.sadness, list(cdu$name, cdu$months, cdu$count, cdu$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "sadness"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$sadness2 <- scale(df_agg$sadness)


fixed8 <- plm(sadness ~ treat_indirect, model="within", index = c("name", "date"), 
              effect = "twoways", data = df_agg)
model8 <- coeftest(fixed8, vcov=vcovHC(fixed8,type="HC0",cluster="group"))

dfdf2 <- broom::tidy(model8)
dfdf2$model <- "sadness"
dfdf2$term <- "CDU/CSU"

df_fesa <- rbind(dfdf1, dfdf2, df_fesa)


df_agg <- aggregate(fdp$el.fear, list(fdp$name, fdp$months, fdp$count, fdp$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "fear"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$fear2 <- scale(df_agg$fear)


fixed9 <- plm(fear ~ treat_indirect, model="within", index = c("name", "date"), 
              effect = "twoways", data = df_agg)
model9 <- coeftest(fixed9, vcov=vcovHC(fixed9,type="HC0",cluster="group"))


dfdf1 <- broom::tidy(model9)
dfdf1$model <- "fear"
dfdf1$term <- "FDP"


df_agg <- aggregate(fdp$el.sadness, list(fdp$name, fdp$months, fdp$count, fdp$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "sadness"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$sadness2 <- scale(df_agg$sadness)


fixed10 <- plm(sadness ~ treat_indirect, model="within", index = c("name", "date"), 
               effect = "twoways", data = df_agg)
model10 <- coeftest(fixed10, vcov=vcovHC(fixed10,type="HC0",cluster="group"))

dfdf2 <- broom::tidy(model10)
dfdf2$model <- "sadness"
dfdf2$term <- "FDP"

df_fesa <- rbind(dfdf1, dfdf2, df_fesa)


df_agg <- aggregate(afd$el.fear, list(afd$name, afd$months, afd$count, afd$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "fear"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$fear2 <- scale(df_agg$fear)


fixed11 <- plm(fear ~ treat_indirect, model="within", index = c("name", "date"), 
               effect = "twoways", data = df_agg)
model11 <- coeftest(fixed11, vcov=vcovHC(fixed11,type="HC0",cluster="group"))


dfdf1 <- broom::tidy(model11)
dfdf1$model <- "fear"
dfdf1$term <- "AfD"


df_agg <- aggregate(afd$el.sadness, list(afd$name, afd$months, afd$count, afd$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "sadness"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$sadness2 <- scale(df_agg$sadness)


fixed12 <- plm(sadness ~ treat_indirect, model="within", index = c("name", "date"), 
               effect = "twoways", data = df_agg)
model12 <- coeftest(fixed12, vcov=vcovHC(fixed12,type="HC0",cluster="group"))

dfdf2 <- broom::tidy(model12)
dfdf2$model <- "sadness"
dfdf2$term <- "AfD"

df_fesa <- rbind(dfdf1, dfdf2, df_fesa)


df_fesa$term <- factor(df_fesa$term, levels=c("The Left", "Greens", "SPD", "CDU/CSU", "FDP", "AfD"))

dwplot(df_fesa, show_intercept = TRUE, ci = .95,
       vline = geom_vline(xintercept = 0, colour = "grey60", linetype = 2)) %>%
  relabel_predictors(c(afd = "AfD", 
                       green = "The Greens",
                       spd = "SPD",
                       cdu = "CDU/CSU",
                       fdp = "FDP",
                       linke = "The Left")) +
  xlab("Coefficient Estimate") + ylab("") +
  scale_colour_manual(name = "Emotion",
                      labels = c("Fear", "Sadness"),
                      values = c("#6699cc", "#CC6666")) +
  theme_bw()

# Exporting figure to .pdf
ggsave("./figures/figureC1.pdf")

#### Figure C2 ###################################################################


df_analysis <- ws_parl


df_analysis$date3 <- as.numeric(df_analysis$date2)-17434
k <- 1
j <- 0
df_analysis$months <- NULL
for (i in seq(from=30, to=1200, by=30)){
  df_analysis$months[df_analysis$date3<i & df_analysis$date3>j] <- k
  j <- i
  k <- k + 1
}


df_analysis2 <- df_analysis[!is.na(df_analysis$wkindirekt),]
df_analysis3 <- df_analysis[df_analysis$wkdirekt!=0,]

df3 <- df_analysis2[df_analysis2$topic==56 | df_analysis2$topic==16,]

df_analysis2$party <- gsub(" ", "", df_analysis2$party)
df_analysis2$party <- gsub("(*UCP)\\s*", "", df_analysis2$party, perl = TRUE)

afd <- df3[!(df3$party!="AfD" & df3$treat_indirect==1),]
green <- df3[!(df3$party!="Greens" & df3$treat_indirect==1),]
spd <- df3[!(df3$party!="SPD" & df3$treat_indirect==1),]
linke <- df3[!(df3$party!="The Left" & df3$treat_indirect==1),]
cdu <- df3[!(df3$party!="CDU/CSU" & df3$treat_indirect==1),]
fdp <- df3[!(df3$party!="FDP" & df3$treat_indirect==1),]


df_positive <- NULL


df_agg <- aggregate(spd$el.joy, list(spd$name, spd$months, spd$count, spd$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "joy"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$joy2 <- scale(df_agg$joy)


fixed1 <- plm(joy ~ treat_indirect, model="within", index = c("name", "date"), 
              effect = "twoways", data = df_agg)


model1 <- coeftest(fixed1, vcov=vcovHC(fixed1,type="HC0",cluster="group"))


dfdf1 <- broom::tidy(model1)
dfdf1$model <- "joy"
dfdf1$term <- "SPD"


df_agg <- aggregate(spd$el.enthusiasm, list(spd$name, spd$months, spd$count, spd$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "enthusiasm"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$enthusiasm2 <- scale(df_agg$enthusiasm)


fixed2 <- plm(enthusiasm ~ treat_indirect, model="within", index = c("name", "date"), 
              effect = "twoways", data = df_agg)


model2 <- coeftest(fixed2, vcov=vcovHC(fixed2,type="HC0",cluster="group"))


dfdf2 <- broom::tidy(model2)
dfdf2$model <- "enthusiasm"
dfdf2$term <- "SPD"


df_agg <- aggregate(spd$el.pride, list(spd$name, spd$months, spd$count, spd$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "pride"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$pride2 <- scale(df_agg$pride)


fixed3 <- plm(pride ~ treat_indirect, model="within", index = c("name", "date"), 
              effect = "twoways", data = df_agg)


model3 <- coeftest(fixed3, vcov=vcovHC(fixed3,type="HC0",cluster="group"))


dfdf3 <- broom::tidy(model3)
dfdf3$model <- "pride"
dfdf3$term <- "SPD"


df_agg <- aggregate(spd$el.hope, list(spd$name, spd$months, spd$count, spd$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "hope"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$hope2 <- scale(df_agg$hope)


fixed4 <- plm(hope ~ treat_indirect, model="within", index = c("name", "date"), 
              effect = "twoways", data = df_agg)


model4 <- coeftest(fixed4, vcov=vcovHC(fixed4,type="HC0",cluster="group"))


dfdf4 <- broom::tidy(model4)
dfdf4$model <- "hope"
dfdf4$term <- "SPD"

df_positive <- rbind(dfdf1, dfdf2, dfdf3, dfdf4, df_positive)



df_agg <- aggregate(linke$el.joy, list(linke$name, linke$months, linke$count, linke$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "joy"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$joy2 <- scale(df_agg$joy)


fixed5 <- plm(joy ~ treat_indirect, model="within", index = c("name", "date"), 
              effect = "twoways", data = df_agg)


model5 <- coeftest(fixed5, vcov=vcovHC(fixed5,type="HC0",cluster="group"))


dfdf1 <- broom::tidy(model5)
dfdf1$model <- "joy"
dfdf1$term <- "The Left"


df_agg <- aggregate(linke$el.enthusiasm, list(linke$name, linke$months, linke$count, linke$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "enthusiasm"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$enthusiasm2 <- scale(df_agg$enthusiasm)


fixed6 <- plm(enthusiasm ~ treat_indirect, model="within", index = c("name", "date"), 
              effect = "twoways", data = df_agg)


model6 <- coeftest(fixed6, vcov=vcovHC(fixed6,type="HC0",cluster="group"))


dfdf2 <- broom::tidy(model6)
dfdf2$model <- "enthusiasm"
dfdf2$term <- "The Left"

df_agg <- aggregate(linke$el.pride, list(linke$name, linke$months, linke$count, linke$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "pride"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$pride2 <- scale(df_agg$pride)


fixed7 <- plm(pride ~ treat_indirect, model="within", index = c("name", "date"), 
              effect = "twoways", data = df_agg)


model7 <- coeftest(fixed7, vcov=vcovHC(fixed7,type="HC0",cluster="group"))


dfdf3 <- broom::tidy(model7)
dfdf3$model <- "pride"
dfdf3$term <- "The Left"


df_agg <- aggregate(linke$el.hope, list(linke$name, linke$months, linke$count, linke$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "hope"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$hope2 <- scale(df_agg$hope)


fixed8 <- plm(hope ~ treat_indirect, model="within", index = c("name", "date"), 
              effect = "twoways", data = df_agg)


model8 <- coeftest(fixed8, vcov=vcovHC(fixed8,type="HC0",cluster="group"))


dfdf4 <- broom::tidy(model8)
dfdf4$model <- "hope"
dfdf4$term <- "The Left"

df_positive <- rbind(dfdf1, dfdf2, dfdf3, dfdf4, df_positive)






df_agg <- aggregate(cdu$el.joy, list(cdu$name, cdu$months, cdu$count, cdu$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "joy"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$joy2 <- scale(df_agg$joy)


fixed9 <- plm(joy ~ treat_indirect, model="within", index = c("name", "date"), 
              effect = "twoways", data = df_agg)


model9 <- coeftest(fixed9, vcov=vcovHC(fixed9,type="HC0",cluster="group"))


dfdf1 <- broom::tidy(model9)
dfdf1$model <- "joy"
dfdf1$term <- "CDU/CSU"


df_agg <- aggregate(cdu$el.enthusiasm, list(cdu$name, cdu$months, cdu$count, cdu$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "enthusiasm"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$enthusiasm2 <- scale(df_agg$enthusiasm)


fixed10 <- plm(enthusiasm ~ treat_indirect, model="within", index = c("name", "date"), 
               effect = "twoways", data = df_agg)


model10 <- coeftest(fixed10, vcov=vcovHC(fixed10,type="HC0",cluster="group"))


dfdf2 <- broom::tidy(model10)
dfdf2$model <- "enthusiasm"
dfdf2$term <- "CDU/CSU"

df_agg <- aggregate(cdu$el.pride, list(cdu$name, cdu$months, cdu$count, cdu$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "pride"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$pride2 <- scale(df_agg$pride)


fixed11 <- plm(pride ~ treat_indirect, model="within", index = c("name", "date"), 
               effect = "twoways", data = df_agg)


model11 <- coeftest(fixed11, vcov=vcovHC(fixed11,type="HC0",cluster="group"))


dfdf3 <- broom::tidy(model11)
dfdf3$model <- "pride"
dfdf3$term <- "CDU/CSU"


df_agg <- aggregate(cdu$el.hope, list(cdu$name, cdu$months, cdu$count, cdu$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "hope"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$hope2 <- scale(df_agg$hope)


fixed12 <- plm(hope ~ treat_indirect, model="within", index = c("name", "date"), 
               effect = "twoways", data = df_agg)


model12 <- coeftest(fixed12, vcov=vcovHC(fixed12,type="HC0",cluster="group"))


dfdf4 <- broom::tidy(model12)
dfdf4$model <- "hope"
dfdf4$term <- "CDU/CSU"

df_positive <- rbind(dfdf1, dfdf2, dfdf3, dfdf4, df_positive)





df_agg <- aggregate(fdp$el.joy, list(fdp$name, fdp$months, fdp$count, fdp$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "joy"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$joy2 <- scale(df_agg$joy)


fixed13 <- plm(joy ~ treat_indirect, model="within", index = c("name", "date"), 
               effect = "twoways", data = df_agg)


model13 <- coeftest(fixed13, vcov=vcovHC(fixed13,type="HC0",cluster="group"))


dfdf1 <- broom::tidy(model13)
dfdf1$model <- "joy"
dfdf1$term <- "FDP"


df_agg <- aggregate(fdp$el.enthusiasm, list(fdp$name, fdp$months, fdp$count, fdp$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "enthusiasm"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$enthusiasm2 <- scale(df_agg$enthusiasm)


fixed14 <- plm(enthusiasm ~ treat_indirect, model="within", index = c("name", "date"), 
               effect = "twoways", data = df_agg)


model14 <- coeftest(fixed14, vcov=vcovHC(fixed14,type="HC0",cluster="group"))


dfdf2 <- broom::tidy(model14)
dfdf2$model <- "enthusiasm"
dfdf2$term <- "FDP"

df_agg <- aggregate(fdp$el.pride, list(fdp$name, fdp$months, fdp$count, fdp$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "pride"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$pride2 <- scale(df_agg$pride)


fixed15 <- plm(pride ~ treat_indirect, model="within", index = c("name", "date"), 
               effect = "twoways", data = df_agg)


model15 <- coeftest(fixed15, vcov=vcovHC(fixed15,type="HC0",cluster="group"))


dfdf3 <- broom::tidy(model15)
dfdf3$model <- "pride"
dfdf3$term <- "FDP"


df_agg <- aggregate(fdp$el.hope, list(fdp$name, fdp$months, fdp$count, fdp$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "hope"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$hope2 <- scale(df_agg$hope)


fixed16 <- plm(hope ~ treat_indirect, model="within", index = c("name", "date"), 
               effect = "twoways", data = df_agg)


model16 <- coeftest(fixed16, vcov=vcovHC(fixed16,type="HC0",cluster="group"))


dfdf4 <- broom::tidy(model16)
dfdf4$model <- "hope"
dfdf4$term <- "FDP"

df_positive <- rbind(dfdf1, dfdf2, dfdf3, dfdf4, df_positive)



df_agg <- aggregate(afd$el.joy, list(afd$name, afd$months, afd$count, afd$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "joy"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$joy2 <- scale(df_agg$joy)


fixed17 <- plm(joy ~ treat_indirect, model="within", index = c("name", "date"), 
               effect = "twoways", data = df_agg)


model17 <- coeftest(fixed17, vcov=vcovHC(fixed17,type="HC0",cluster="group"))


dfdf1 <- broom::tidy(model17)
dfdf1$model <- "joy"
dfdf1$term <- "AfD"


df_agg <- aggregate(afd$el.enthusiasm, list(afd$name, afd$months, afd$count, afd$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "enthusiasm"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$enthusiasm2 <- scale(df_agg$enthusiasm)


fixed18 <- plm(enthusiasm ~ treat_indirect, model="within", index = c("name", "date"), 
               effect = "twoways", data = df_agg)


model18 <- coeftest(fixed18, vcov=vcovHC(fixed18,type="HC0",cluster="group"))


dfdf2 <- broom::tidy(model18)
dfdf2$model <- "enthusiasm"
dfdf2$term <- "AfD"


df_agg <- aggregate(afd$el.pride, list(afd$name, afd$months, afd$count, afd$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "pride"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$pride2 <- scale(df_agg$pride)


fixed19 <- plm(pride ~ treat_indirect, model="within", index = c("name", "date"), 
               effect = "twoways", data = df_agg)


model19 <- coeftest(fixed19, vcov=vcovHC(fixed19,type="HC0",cluster="group"))


dfdf3 <- broom::tidy(model19)
dfdf3$model <- "pride"
dfdf3$term <- "AfD"


df_agg <- aggregate(afd$el.hope, list(afd$name, afd$months, afd$count, afd$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "hope"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$hope2 <- scale(df_agg$hope)


fixed20 <- plm(hope ~ treat_indirect, model="within", index = c("name", "date"), 
               effect = "twoways", data = df_agg)


model20 <- coeftest(fixed20, vcov=vcovHC(fixed20,type="HC0",cluster="group"))


dfdf4 <- broom::tidy(model20)
dfdf4$model <- "hope"
dfdf4$term <- "AfD"

df_positive <- rbind(dfdf1, dfdf2, dfdf3, dfdf4, df_positive)




df_agg <- aggregate(green$el.joy, list(green$name, green$months, green$count, green$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "joy"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$joy2 <- scale(df_agg$joy)


fixed21 <- plm(joy ~ treat_indirect, model="within", index = c("name", "date"), 
               effect = "twoways", data = df_agg)


model21 <- coeftest(fixed21, vcov=vcovHC(fixed21,type="HC0",cluster="group"))


dfdf1 <- broom::tidy(model21)
dfdf1$model <- "joy"
dfdf1$term <- "Greens"


df_agg <- aggregate(green$el.enthusiasm, list(green$name, green$months, green$count, green$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "enthusiasm"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$enthusiasm2 <- scale(df_agg$enthusiasm)


fixed22 <- plm(enthusiasm ~ treat_indirect, model="within", index = c("name", "date"), 
               effect = "twoways", data = df_agg)


model22 <- coeftest(fixed22, vcov=vcovHC(fixed22,type="HC0",cluster="group"))


dfdf2 <- broom::tidy(model22)
dfdf2$model <- "enthusiasm"
dfdf2$term <- "Greens"

df_agg <- aggregate(green$el.pride, list(green$name, green$months, green$count, green$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "pride"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$pride2 <- scale(df_agg$pride)


fixed23 <- plm(pride ~ treat_indirect, model="within", index = c("name", "date"), 
               effect = "twoways", data = df_agg)


model23 <- coeftest(fixed23, vcov=vcovHC(fixed23,type="HC0",cluster="group"))


dfdf3 <- broom::tidy(model23)
dfdf3$model <- "pride"
dfdf3$term <- "Greens"


df_agg <- aggregate(green$el.hope, list(green$name, green$months, green$count, green$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "hope"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$hope2 <- scale(df_agg$hope)


fixed24 <- plm(hope ~ treat_indirect, model="within", index = c("name", "date"), 
               effect = "twoways", data = df_agg)


model24 <- coeftest(fixed24, vcov=vcovHC(fixed24,type="HC0",cluster="group"))


dfdf4 <- broom::tidy(model24)
dfdf4$model <- "hope"
dfdf4$term <- "Greens"

df_positive <- rbind(dfdf1, dfdf2, dfdf3, dfdf4, df_positive)


df_positive$term <- factor(df_positive$term, levels=c("The Left", "Greens", "SPD", "CDU/CSU", "FDP", "AfD"))


dwplot(df_positive, show_intercept = TRUE, ci = .95,
       vline = geom_vline(xintercept = 0, colour = "grey60", linetype = 2)) %>%
  relabel_predictors(c(fdp = "FDP", 
                       cdu = "CDU/CSU", 
                       linke = "The Left", 
                       spd = "SPD",
                       afd = "AfD",
                       green = "The Greens")) +
  xlab("Coefficient Estimate") + ylab("") +
  scale_colour_manual(name = "Emotion",
                      labels = c("Joy", "Enthusiasm", "Pride", "Hope"),
                      values = c("#6699cc", "#CC6666", "#66CC99", "#FFA38C")) +
  theme_bw()

# Exporting figure to .pdf
ggsave("./figures/figureC2.pdf")

#### Figure C3 #####################################################################

# Convert 'date2' to a numeric format, subtract a constant (17434), and store the result in a new column 'date3'.
ws_parl$date3 <- as.numeric(ws_parl$date2) - 17434

# Initialize counters.
k <- 1
j <- 0

# Initialize a new column 'months' with NULL values in the 'ws_parl' dataframe.
ws_parl$months <- NULL

# Segment the 'date3' column into 40 intervals (or 'months') of 30 units each.
for (i in seq(from = 30, to = 1200, by = 30)) {
  ws_parl$months[ws_parl$date3 < i & ws_parl$date3 > j] <- k
  j <- i
  k <- k + 1
}

# Count the number of rows for each combination of 'months' and 'party' and store the result in a new column 'number'.
df_analysis <- ws_parl %>% add_count(months, party)
names(df_analysis)[names(df_analysis) == "n"] <- "number"

# Count the number of rows where 'twoabove7' is TRUE for each combination of 'months' and 'party'.
df_analysis <- df_analysis %>% add_count(months, party, wt = twoabove7)

# Compute the keyword salience by dividing the count of rows where 'twoabove7' is TRUE by the total number of rows.
df_analysis$keyword_salience <- df_analysis$n / df_analysis$number

# Group by 'treat_indirect' and compute summary statistics for 'keyword_salience'. Also, calculate the standard error and 95% confidence intervals.
df22 <- df_analysis %>%
  group_by(treat_indirect) %>%
  dplyr::summarize(t_prop = mean(keyword_salience, na.rm = T),
                   sd_tp = sd(keyword_salience, na.rm = TRUE),
                   n_tp = n()) %>%
  mutate(se_tp = sd_tp / sqrt(n_tp),
         lower.ci = t_prop - qt(1 - (0.05 / 2), n_tp - 1) * se_tp,
         upper.ci = t_prop + qt(1 - (0.05 / 2), n_tp - 1) * se_tp)

# Visualize the mean 'keyword_salience' for each 'treat_indirect' group with 95% confidence intervals.
plot22 <- df22 %>%
  ggplot(aes(t_prop, as.factor(treat_indirect))) +
  geom_point(color = "#CC6666") +
  geom_errorbarh(aes(xmin = lower.ci, xmax = upper.ci), height = .1, color = "#CC6666") +
  theme_bw() +
  labs(x = "Topic Proportion", y = "Treatment")

# Group by 'party' and compute summary statistics for 'keyword_salience'. Also, calculate the standard error and 95% confidence intervals.
df22_party <- df_analysis %>%
  group_by(party) %>%
  dplyr::summarize(t_prop = mean(keyword_salience, na.rm = T),
                   sd_tp = sd(keyword_salience, na.rm = TRUE),
                   n_tp = n()) %>%
  mutate(se_tp = sd_tp / sqrt(n_tp),
         lower.ci = t_prop - qt(1 - (0.05 / 2), n_tp - 1) * se_tp,
         upper.ci = t_prop + qt(1 - (0.05 / 2), n_tp - 1) * se_tp)

# Visualize the mean 'keyword_salience' for each 'party' with 95% confidence intervals.
plot22_party <- df22_party %>%
  ggplot(aes(t_prop, as.factor(party))) +
  geom_point(color = "#CC6666") +
  geom_errorbarh(aes(xmin = lower.ci, xmax = upper.ci), height = .1, color = "#CC6666") +
  theme_bw() +
  labs(x = "Topic Proportion", y = "Party")

# Arrange and display the two plots (for 'treat_indirect' and 'party') side by side.
ggarrange(plot22, plot22_party,
          ncol = 2, nrow = 1)

# Exporting figure to .pdf
ggsave("./figures/figureC3.pdf")


#### Figure C4 ###################################################################

# Group the df_analysis dataframe by treat_indirect and party.
# For each group, compute the mean, standard deviation, and count of keyword_salience.
# Additionally, compute the standard error and 95% confidence intervals for the mean keyword_salience.
df22 <- df_analysis %>%
  group_by(treat_indirect, party) %>%
  dplyr::summarize(
    t_prop = mean(keyword_salience, na.rm = T),
    sd_tp = sd(keyword_salience, na.rm = TRUE),
    n_tp = n()
  ) %>%
  mutate(
    se_tp = sd_tp / sqrt(n_tp),
    lower.ci = t_prop - qt(1 - (0.05 / 2), n_tp - 1) * se_tp,
    upper.ci = t_prop + qt(1 - (0.05 / 2), n_tp - 1) * se_tp
  )

# Visualize the mean keyword_salience for each combination of treat_indirect and party.
# The visualization includes 95% confidence intervals for each group.
# The groups are separated into individual facets based on party, with parties presented in reverse order.
df22 %>%
  ggplot(aes(t_prop, as.factor(treat_indirect), group = party)) +
  geom_point(color = "#CC6666") +
  geom_errorbarh(aes(xmin = lower.ci, xmax = upper.ci), height = .1, color = "#CC6666") +
  theme_bw() +
  facet_wrap(~fct_rev(party)) +
  labs(x = "Topic Proportion", y = "Treatment")

# Exporting figure to .pdf
ggsave("./figures/figureC4.pdf")

#### Figure C5 ###################################################################


ws_parl2 <- ws_parl[!is.na(ws_parl$wkindirekt),]

df_analysis <- ws_parl2[ws_parl2$twoabove7==1,]

afd <- df_analysis[!(df_analysis$party!="AfD" & df_analysis$treat_indirect==1),]
green <- df_analysis[!(df_analysis$party!="Greens" & df_analysis$treat_indirect==1),]
spd <- df_analysis[!(df_analysis$party!="SPD" & df_analysis$treat_indirect==1),]
linke <- df_analysis[!(df_analysis$party!="The Left" & df_analysis$treat_indirect==1),]
cdu <- df_analysis[!(df_analysis$party!="CDU/CSU" & df_analysis$treat_indirect==1),]
fdp <- df_analysis[!(df_analysis$party!="FDP" & df_analysis$treat_indirect==1),]

df_agg <- aggregate(green$el.anger, list(green$name, green$months, green$count, green$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "anger"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$anger2 <- scale(df_agg$anger)

fixed1 <- plm(anger ~ treat_indirect, model="within", index = c("name", "date"), 
              effect = "twoways", data = df_agg)
model1 <- coeftest(fixed1, vcov=vcovHC(fixed1,type="HC0",cluster="group"))

dfdf1 <- broom::tidy(model1)
dfdf1$model <- "Anger"
dfdf1$term <- "Greens"

df_agg <- aggregate(green$el.disgust, list(green$name, green$months, green$count, green$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "disgust"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$disgust2 <- scale(df_agg$disgust)

fixed2 <- plm(disgust ~ treat_indirect, model="within", index = c("name", "date"), 
              effect = "twoways", data = df_agg)
model2 <- coeftest(fixed2, vcov=vcovHC(fixed2,type="HC0",cluster="group"))

dfdf2 <- broom::tidy(model2)
dfdf2$model <- "Disgust"
dfdf2$term <- "Greens"



df_agg <- aggregate(afd$el.anger, list(afd$name, afd$months, afd$count, afd$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "anger"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$anger2 <- scale(df_agg$anger)

fixed3 <- plm(anger ~ treat_indirect, model="within", index = c("name", "date"), 
              effect = "twoways", data = df_agg)
model3 <- coeftest(fixed3, vcov=vcovHC(fixed3,type="HC0",cluster="group"))

dfdf3 <- broom::tidy(model3)
dfdf3$model <- "Anger"
dfdf3$term <- "AfD"

df_agg <- aggregate(afd$el.disgust, list(afd$name, afd$months, afd$count, afd$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "disgust"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$disgust2 <- scale(df_agg$disgust)

fixed4 <- plm(disgust ~ treat_indirect, model="within", index = c("name", "date"), 
              effect = "twoways", data = df_agg)
model4 <- coeftest(fixed4, vcov=vcovHC(fixed4,type="HC0",cluster="group"))

dfdf4 <- broom::tidy(model4)
dfdf4$model <- "Disgust"
dfdf4$term <- "AfD"



df_agg <- aggregate(cdu$el.anger, list(cdu$name, cdu$months, cdu$count, cdu$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "anger"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$anger2 <- scale(df_agg$anger)

fixed5 <- plm(anger ~ treat_indirect, model="within", index = c("name", "date"), 
              effect = "twoways", data = df_agg)
model5 <- coeftest(fixed5, vcov=vcovHC(fixed5,type="HC0",cluster="group"))

dfdf5 <- broom::tidy(model5)
dfdf5$model <- "Anger"
dfdf5$term <- "CDU/CSU"

df_agg <- aggregate(cdu$el.disgust, list(cdu$name, cdu$months, cdu$count, cdu$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "disgust"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$disgust2 <- scale(df_agg$disgust)

fixed6 <- plm(disgust ~ treat_indirect, model="within", index = c("name", "date"), 
              effect = "twoways", data = df_agg)
model6 <- coeftest(fixed6, vcov=vcovHC(fixed6,type="HC0",cluster="group"))

dfdf6 <- broom::tidy(model6)
dfdf6$model <- "Disgust"
dfdf6$term <- "CDU/CSU"



df_agg <- aggregate(fdp$el.anger, list(fdp$name, fdp$months, fdp$count, fdp$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "anger"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$anger2 <- scale(df_agg$anger)

fixed5 <- plm(anger ~ treat_indirect, model="within", index = c("name", "date"), 
              effect = "twoways", data = df_agg)
model5 <- coeftest(fixed5, vcov=vcovHC(fixed5,type="HC0",cluster="group"))

dfdf7 <- broom::tidy(model5)
dfdf7$model <- "Anger"
dfdf7$term <- "FDP"

df_agg <- aggregate(fdp$el.disgust, list(fdp$name, fdp$months, fdp$count, fdp$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "disgust"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$disgust2 <- scale(df_agg$disgust)

fixed6 <- plm(disgust ~ treat_indirect, model="within", index = c("name", "date"), 
              effect = "twoways", data = df_agg)
model6 <- coeftest(fixed6, vcov=vcovHC(fixed6,type="HC0",cluster="group"))

dfdf8 <- broom::tidy(model6)
dfdf8$model <- "Disgust"
dfdf8$term <- "FDP"

df_agg <- aggregate(spd$el.anger, list(spd$name, spd$months, spd$count, spd$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "anger"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$anger2 <- scale(df_agg$anger)

fixed5 <- plm(anger ~ treat_indirect, model="within", index = c("name", "date"), 
              effect = "twoways", data = df_agg)
model5 <- coeftest(fixed5, vcov=vcovHC(fixed5,type="HC0",cluster="group"))

dfdf9 <- broom::tidy(model5)
dfdf9$model <- "Anger"
dfdf9$term <- "SPD"

df_agg <- aggregate(spd$el.disgust, list(spd$name, spd$months, spd$count, spd$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "disgust"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$disgust2 <- scale(df_agg$disgust)

fixed6 <- plm(disgust ~ treat_indirect, model="within", index = c("name", "date"), 
              effect = "twoways", data = df_agg)
model6 <- coeftest(fixed6, vcov=vcovHC(fixed6,type="HC0",cluster="group"))

dfdf10 <- broom::tidy(model6)
dfdf10$model <- "Disgust"
dfdf10$term <- "SPD"


df_agg <- aggregate(linke$el.anger, list(linke$name, linke$months, linke$count, linke$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "anger"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$anger2 <- scale(df_agg$anger)

fixed5 <- plm(anger ~ treat_indirect, model="within", index = c("name", "date"), 
              effect = "twoways", data = df_agg)
model5 <- coeftest(fixed5, vcov=vcovHC(fixed5,type="HC0",cluster="group"))

dfdf11 <- broom::tidy(model5)
dfdf11$model <- "Anger"
dfdf11$term <- "The Left"

df_agg <- aggregate(linke$el.disgust, list(linke$name, linke$months, linke$count, linke$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "disgust"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$disgust2 <- scale(df_agg$disgust)

fixed6 <- plm(disgust ~ treat_indirect, model="within", index = c("name", "date"), 
              effect = "twoways", data = df_agg)
model6 <- coeftest(fixed6, vcov=vcovHC(fixed6,type="HC0",cluster="group"))

dfdf12 <- broom::tidy(model6)
dfdf12$model <- "Disgust"
dfdf12$term <- "The Left"




df_anger <- rbind(dfdf1, dfdf3, dfdf5, dfdf7, dfdf9, dfdf11)

df_disgust <- rbind(dfdf2, dfdf4, dfdf6, dfdf8, dfdf10, dfdf12)

df_anger$term <- factor(df_anger$term, levels=c("The Left", "Greens", "SPD", "CDU/CSU", "FDP", "AfD"))

df_disgust$term <- factor(df_disgust$term, levels=c("The Left", "Greens", "SPD", "CDU/CSU", "FDP", "AfD"))

anger_coef <- dwplot(df_anger, show_intercept = TRUE, ci = .95,
                     vline = geom_vline(xintercept = 0, colour = "grey60", linetype = 2)) %>%
  relabel_predictors(c(fdp = "FDP", 
                       cdu = "CDU/CSU", 
                       linke = "Die Linke", 
                       spd = "SPD")) +
  xlab("Coefficient Estimate (Anger)") + ylab("") +
  scale_colour_manual(values = c("#6699cc")) +
  guides(colour = "none") +
  theme_bw()

disgust_coef <- dwplot(df_disgust, show_intercept = TRUE, ci = .95,
                       vline = geom_vline(xintercept = 0, colour = "grey60", linetype = 2)) %>%
  relabel_predictors(c(fdp = "FDP", 
                       cdu = "CDU/CSU", 
                       linke = "Die Linke", 
                       spd = "SPD")) +
  xlab("Coefficient Estimate (Disgust)") + ylab("") +
  scale_colour_manual(values = c("#CC6666")) +
  guides(colour = "none") +
  theme_bw()

ggpubr::ggarrange(anger_coef, disgust_coef)

# Exporting figure to .pdf
ggsave("./figures/figureC5.pdf")

#### Figure D1 ##################################################################

wind_tweets$date2 <- as.numeric(wind_tweets$date)
wind_tweets$date3 <- as.numeric(wind_tweets$date2)-17434
k <- 1
j <- 0
wind_tweets$months <- NULL
for (i in seq(from=30, to=1200, by=30)){
  wind_tweets$months[wind_tweets$date3<=i & wind_tweets$date3>=j] <- k
  j <- i
  k <- k + 1
}

wind_tweets2 <- wind_tweets[!is.na(wind_tweets$wkindirekt),]
wind_tweets2 <- dplyr::filter(wind_tweets2, is_retweet == 0)

df3 <- wind_tweets2[wind_tweets2$topic==46,]

afd <- df3[!(df3$party!="AfD" & df3$treat_indirect==1),]
green <- df3[!(df3$party!="Greens" & df3$treat_indirect==1),]
spd <- df3[!(df3$party!="SPD" & df3$treat_indirect==1),]
linke <- df3[!(df3$party!="The Left" & df3$treat_indirect==1),]
cdu <- df3[!(df3$party!="CDU/CSU" & df3$treat_indirect==1),]
fdp <- df3[!(df3$party!="FDP" & df3$treat_indirect==1),]




df_agg <- aggregate(green$el.anger, list(green$username, green$months, green$count, green$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "anger"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$anger2 <- scale(df_agg$anger)

fixed1 <- plm(anger ~ treat_indirect, model="within", index = c("name", "date"), 
              effect = "twoways", data = df_agg)
model1 <- coeftest(fixed1, vcov=vcovHC(fixed1,type="HC0",cluster="group"))

dfdf1 <- broom::tidy(model1)
dfdf1$model <- "Anger"
dfdf1$term <- "Greens"

df_agg <- aggregate(green$el.disgust, list(green$username, green$months, green$count, green$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "disgust"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$disgust2 <- scale(df_agg$disgust)

fixed2 <- plm(disgust ~ treat_indirect, model="within", index = c("name", "date"), 
              effect = "twoways", data = df_agg)
model2 <- coeftest(fixed2, vcov=vcovHC(fixed2,type="HC0",cluster="group"))

dfdf2 <- broom::tidy(model2)
dfdf2$model <- "Disgust"
dfdf2$term <- "Greens"



df_agg <- aggregate(afd$el.anger, list(afd$username, afd$months, afd$count, afd$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "anger"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$anger2 <- scale(df_agg$anger)

fixed1 <- plm(anger ~ treat_indirect, model="within", index = c("name", "date"), 
              effect = "twoways", data = df_agg)
model1 <- coeftest(fixed1, vcov=vcovHC(fixed1,type="HC0",cluster="group"))

dfdf3 <- broom::tidy(model1)
dfdf3$model <- "Anger"
dfdf3$term <- "AfD"

df_agg <- aggregate(afd$el.disgust, list(afd$username, afd$months, afd$count, afd$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "disgust"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$disgust2 <- scale(df_agg$disgust)

fixed2 <- plm(disgust ~ treat_indirect, model="within", index = c("name", "date"), 
              effect = "twoways", data = df_agg)
model2 <- coeftest(fixed2, vcov=vcovHC(fixed2,type="HC0",cluster="group"))

dfdf4 <- broom::tidy(model2)
dfdf4$model <- "Disgust"
dfdf4$term <- "AfD"



df_agg <- aggregate(cdu$el.anger, list(cdu$username, cdu$months, cdu$count, cdu$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "anger"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$anger2 <- scale(df_agg$anger)

fixed1 <- plm(anger ~ treat_indirect, model="within", index = c("name", "date"), 
              effect = "twoways", data = df_agg)
model1 <- coeftest(fixed1, vcov=vcovHC(fixed1,type="HC0",cluster="group"))

dfdf5 <- broom::tidy(model1)
dfdf5$model <- "Anger"
dfdf5$term <- "CDU/CSU"

df_agg <- aggregate(cdu$el.disgust, list(cdu$username, cdu$months, cdu$count, cdu$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "disgust"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$disgust2 <- scale(df_agg$disgust)

fixed2 <- plm(disgust ~ treat_indirect, model="within", index = c("name", "date"), 
              effect = "twoways", data = df_agg)
model2 <- coeftest(fixed2, vcov=vcovHC(fixed2,type="HC0",cluster="group"))

dfdf6 <- broom::tidy(model2)
dfdf6$model <- "Disgust"
dfdf6$term <- "CDU/CSU"



df_agg <- aggregate(fdp$el.anger, list(fdp$username, fdp$months, fdp$count, fdp$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "anger"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$anger2 <- scale(df_agg$anger)

fixed1 <- plm(anger ~ treat_indirect, model="within", index = c("name", "date"), 
              effect = "twoways", data = df_agg)
model1 <- coeftest(fixed2, vcov=vcovHC(fixed1,type="HC0",cluster="group"))

dfdf7 <- broom::tidy(model1)
dfdf7$model <- "Anger"
dfdf7$term <- "FDP"

df_agg <- aggregate(fdp$el.disgust, list(fdp$username, fdp$months, fdp$count, fdp$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "disgust"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$disgust2 <- scale(df_agg$disgust)

fixed2 <- plm(disgust ~ treat_indirect, model="within", index = c("name", "date"), 
              effect = "twoways", data = df_agg)
model2 <- coeftest(fixed2, vcov=vcovHC(fixed2,type="HC0",cluster="group"))

dfdf8 <- broom::tidy(model2)
dfdf8$model <- "Disgust"
dfdf8$term <- "FDP"


df_agg <- aggregate(spd$el.anger, list(spd$username, spd$months, spd$count, spd$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "anger"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$anger2 <- scale(df_agg$anger)

fixed1 <- plm(anger ~ treat_indirect, model="within", index = c("name", "date"), 
              effect = "twoways", data = df_agg)
model1 <- coeftest(fixed2, vcov=vcovHC(fixed1,type="HC0",cluster="group"))

dfdf9 <- broom::tidy(model1)
dfdf9$model <- "Anger"
dfdf9$term <- "SPD"

df_agg <- aggregate(spd$el.disgust, list(spd$username, spd$months, spd$count, spd$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "disgust"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$disgust2 <- scale(df_agg$disgust)

fixed2 <- plm(disgust ~ treat_indirect, model="within", index = c("name", "date"), 
              effect = "twoways", data = df_agg)
model2 <- coeftest(fixed2, vcov=vcovHC(fixed2,type="HC0",cluster="group"))

dfdf10 <- broom::tidy(model2)
dfdf10$model <- "Disgust"
dfdf10$term <- "SPD"


df_agg <- aggregate(linke$el.anger, list(linke$username, linke$months, linke$count, linke$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "anger"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$anger2 <- scale(df_agg$anger)

fixed11 <- plm(anger ~ treat_indirect, model="within", index = c("name", "date"), 
              effect = "twoways", data = df_agg)
model1 <- coeftest(fixed1, vcov=vcovHC(fixed1,type="HC0",cluster="group"))

dfdf11 <- broom::tidy(model1)
dfdf11$model <- "Anger"
dfdf11$term <- "The Left"

df_agg <- aggregate(linke$el.disgust, list(linke$username, linke$months, linke$count, linke$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "disgust"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$disgust2 <- scale(df_agg$disgust)

fixed2 <- plm(disgust ~ treat_indirect, model="within", index = c("name", "date"), 
              effect = "twoways", data = df_agg)
model2 <- coeftest(fixed2, vcov=vcovHC(fixed2,type="HC0",cluster="group"))

dfdf12 <- broom::tidy(model2)
dfdf12$model <- "Disgust"
dfdf12$term <- "The Left"



df_anger <- rbind(dfdf11, dfdf1, dfdf9, dfdf5, dfdf7,  dfdf3)

df_disgust <- rbind(dfdf12, dfdf2, dfdf10, dfdf6, dfdf8,  dfdf4)

anger_coef <- dwplot(df_anger, show_intercept = TRUE, ci = .95,
                     vline = geom_vline(xintercept = 0, colour = "grey60", linetype = 2)) %>%
  relabel_predictors(c(fdp = "FDP", 
                       cdu = "CDU/CSU", 
                       linke = "Die Linke", 
                       spd = "SPD")) +
  xlab("Coefficient Estimate (Anger)") + ylab("") +
  scale_colour_manual(values = c("#6699cc")) +
  guides(colour = "none") +
  theme_bw()

disgust_coef <- dwplot(df_disgust, show_intercept = TRUE, ci = .95,
                       vline = geom_vline(xintercept = 0, colour = "grey60", linetype = 2)) %>%
  relabel_predictors(c(fdp = "FDP", 
                       cdu = "CDU/CSU", 
                       linke = "Die Linke", 
                       spd = "SPD")) +
  xlab("Coefficient Estimate (Disgust)") + ylab("") +
  scale_colour_manual(values = c("#CC6666")) +
  guides(colour = "none") +
  theme_bw()

ggpubr::ggarrange(anger_coef, disgust_coef)

# Exporting figure to .pdf
ggsave("./figures/figureD1.pdf")


#### Figure D2 ####################################################################


df_fesa <- NULL

df_agg <- aggregate(linke$el.fear, list(linke$username, linke$months, linke$count, linke$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "fear"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$fear2 <- scale(df_agg$fear)


fixed1 <- plm(fear ~ treat_indirect, model="within", index = c("name", "date"), 
              effect = "twoways", data = df_agg)
model1 <- coeftest(fixed1, vcov=vcovHC(fixed1,type="HC0",cluster="group"))


dfdf1 <- broom::tidy(model1)
dfdf1$model <- "fear"
dfdf1$term <- "The Left"


df_agg <- aggregate(linke$el.sadness, list(linke$username, linke$months, linke$count, linke$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "sadness"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$sadness2 <- scale(df_agg$sadness)


fixed1 <- plm(sadness ~ treat_indirect, model="within", index = c("name", "date"), 
              effect = "twoways", data = df_agg)
model2 <- coeftest(fixed1, vcov=vcovHC(fixed1,type="HC0",cluster="group"))

dfdf2 <- broom::tidy(model2)
dfdf2$model <- "sadness"
dfdf2$term <- "The Left"

df_fesa <- rbind(dfdf1, dfdf2, df_fesa)


df_agg <- aggregate(green$el.fear, list(green$username, green$months, green$count, green$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "fear"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$fear2 <- scale(df_agg$fear)


fixed1 <- plm(fear ~ treat_indirect, model="within", index = c("name", "date"), 
              effect = "twoways", data = df_agg)
model1 <- coeftest(fixed1, vcov=vcovHC(fixed1,type="HC0",cluster="group"))


dfdf1 <- broom::tidy(model1)
dfdf1$model <- "fear"
dfdf1$term <- "Greens"


df_agg <- aggregate(green$el.sadness, list(green$username, green$months, green$count, green$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "sadness"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$sadness2 <- scale(df_agg$sadness)


fixed1 <- plm(sadness ~ treat_indirect, model="within", index = c("name", "date"), 
              effect = "twoways", data = df_agg)
model2 <- coeftest(fixed1, vcov=vcovHC(fixed1,type="HC0",cluster="group"))

dfdf2 <- broom::tidy(model2)
dfdf2$model <- "sadness"
dfdf2$term <- "Greens"

df_fesa <- rbind(dfdf1, dfdf2, df_fesa)


df_agg <- aggregate(spd$el.fear, list(spd$username, spd$months, spd$count, spd$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "fear"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$fear2 <- scale(df_agg$fear)


fixed1 <- plm(fear ~ treat_indirect, model="within", index = c("name", "date"), 
              effect = "twoways", data = df_agg)
model1 <- coeftest(fixed1, vcov=vcovHC(fixed1,type="HC0",cluster="group"))


dfdf1 <- broom::tidy(model1)
dfdf1$model <- "fear"
dfdf1$term <- "SPD"


df_agg <- aggregate(spd$el.sadness, list(spd$username, spd$months, spd$count, spd$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "sadness"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$sadness2 <- scale(df_agg$sadness)


fixed1 <- plm(sadness ~ treat_indirect, model="within", index = c("name", "date"), 
              effect = "twoways", data = df_agg)
model2 <- coeftest(fixed1, vcov=vcovHC(fixed1,type="HC0",cluster="group"))

dfdf2 <- broom::tidy(model2)
dfdf2$model <- "sadness"
dfdf2$term <- "SPD"

df_fesa <- rbind(dfdf1, dfdf2, df_fesa)



df_agg <- aggregate(cdu$el.fear, list(cdu$username, cdu$months, cdu$count, cdu$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "fear"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$fear2 <- scale(df_agg$fear)


fixed1 <- plm(fear ~ treat_indirect, model="within", index = c("name", "date"), 
              effect = "twoways", data = df_agg)
model1 <- coeftest(fixed1, vcov=vcovHC(fixed1,type="HC0",cluster="group"))


dfdf1 <- broom::tidy(model1)
dfdf1$model <- "fear"
dfdf1$term <- "CDU/CSU"


df_agg <- aggregate(cdu$el.sadness, list(cdu$username, cdu$months, cdu$count, cdu$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "sadness"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$sadness2 <- scale(df_agg$sadness)


fixed1 <- plm(sadness ~ treat_indirect, model="within", index = c("name", "date"), 
              effect = "twoways", data = df_agg)
model2 <- coeftest(fixed1, vcov=vcovHC(fixed1,type="HC0",cluster="group"))

dfdf2 <- broom::tidy(model2)
dfdf2$model <- "sadness"
dfdf2$term <- "CDU/CSU"

df_fesa <- rbind(dfdf1, dfdf2, df_fesa)


df_agg <- aggregate(fdp$el.fear, list(fdp$username, fdp$months, fdp$count, fdp$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "fear"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$fear2 <- scale(df_agg$fear)


fixed1 <- plm(fear ~ treat_indirect, model="within", index = c("name", "date"), 
              effect = "twoways", data = df_agg)
model1 <- coeftest(fixed1, vcov=vcovHC(fixed1,type="HC0",cluster="group"))


dfdf1 <- broom::tidy(model1)
dfdf1$model <- "fear"
dfdf1$term <- "FDP"


df_agg <- aggregate(fdp$el.sadness, list(fdp$username, fdp$months, fdp$count, fdp$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "sadness"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$sadness2 <- scale(df_agg$sadness)


fixed1 <- plm(sadness ~ treat_indirect, model="within", index = c("name", "date"), 
              effect = "twoways", data = df_agg)
model2 <- coeftest(fixed1, vcov=vcovHC(fixed1,type="HC0",cluster="group"))

dfdf2 <- broom::tidy(model2)
dfdf2$model <- "sadness"
dfdf2$term <- "FDP"

df_fesa <- rbind(dfdf1, dfdf2, df_fesa)


df_agg <- aggregate(afd$el.fear, list(afd$username, afd$months, afd$count, afd$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "fear"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$fear2 <- scale(df_agg$fear)


fixed1 <- plm(fear ~ treat_indirect, model="within", index = c("name", "date"), 
              effect = "twoways", data = df_agg)
model1 <- coeftest(fixed1, vcov=vcovHC(fixed1,type="HC0",cluster="group"))


dfdf1 <- broom::tidy(model1)
dfdf1$model <- "fear"
dfdf1$term <- "AfD"


df_agg <- aggregate(afd$el.sadness, list(afd$username, afd$months, afd$count, afd$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "sadness"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$sadness2 <- scale(df_agg$sadness)


fixed1 <- plm(sadness ~ treat_indirect, model="within", index = c("name", "date"), 
              effect = "twoways", data = df_agg)
model2 <- coeftest(fixed1, vcov=vcovHC(fixed1,type="HC0",cluster="group"))

dfdf2 <- broom::tidy(model2)
dfdf2$model <- "sadness"
dfdf2$term <- "AfD"

df_fesa <- rbind(dfdf1, dfdf2, df_fesa)


df_fesa$term <- factor(df_fesa$term, levels=c("The Left", "Greens", "SPD", "CDU/CSU", "FDP", "AfD"))

dwplot(df_fesa, show_intercept = TRUE, ci = .95,
       vline = geom_vline(xintercept = 0, colour = "grey60", linetype = 2)) %>%
  relabel_predictors(c(afd = "AfD", 
                       green = "The Greens",
                       spd = "SPD",
                       cdu = "CDU/CSU",
                       fdp = "FDP",
                       linke = "The Left")) +
  xlab("Coefficient Estimate") + ylab("") +
  scale_colour_manual(name = "Emotion",
                      labels = c("Fear", "Sadness"),
                      values = c("#6699cc", "#CC6666")) +
  theme_bw()


# Exporting figure to .pdf
ggsave("./figures/figureD2.pdf")

#### Figure D3 ###############################################################

df_positive <- NULL

df_agg <- aggregate(spd$el.joy, list(spd$username, spd$months, spd$count, spd$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "joy"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$joy2 <- scale(df_agg$joy)


fixed1 <- plm(joy ~ treat_indirect, model="within", index = c("name", "date"), 
              effect = "twoways", data = df_agg)


model1 <- coeftest(fixed1, vcov=vcovHC(fixed1,type="HC0",cluster="group"))


dfdf1 <- broom::tidy(model1)
dfdf1$model <- "joy"
dfdf1$term <- "SPD"


df_agg <- aggregate(spd$el.enthusiasm, list(spd$username, spd$months, spd$count, spd$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "enthusiasm"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$enthusiasm2 <- scale(df_agg$enthusiasm)


fixed1 <- plm(enthusiasm ~ treat_indirect, model="within", index = c("name", "date"), 
              effect = "twoways", data = df_agg)


model1 <- coeftest(fixed1, vcov=vcovHC(fixed1,type="HC0",cluster="group"))


dfdf2 <- broom::tidy(model1)
dfdf2$model <- "enthusiasm"
dfdf2$term <- "SPD"


df_agg <- aggregate(spd$el.pride, list(spd$username, spd$months, spd$count, spd$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "pride"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$pride2 <- scale(df_agg$pride)


fixed1 <- plm(pride ~ treat_indirect, model="within", index = c("name", "date"), 
              effect = "twoways", data = df_agg)


model1 <- coeftest(fixed1, vcov=vcovHC(fixed1,type="HC0",cluster="group"))


dfdf3 <- broom::tidy(model1)
dfdf3$model <- "pride"
dfdf3$term <- "SPD"


df_agg <- aggregate(spd$el.hope, list(spd$username, spd$months, spd$count, spd$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "hope"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$hope2 <- scale(df_agg$hope)


fixed1 <- plm(hope ~ treat_indirect, model="within", index = c("name", "date"), 
              effect = "twoways", data = df_agg)


model1 <- coeftest(fixed1, vcov=vcovHC(fixed1,type="HC0",cluster="group"))


dfdf4 <- broom::tidy(model1)
dfdf4$model <- "hope"
dfdf4$term <- "SPD"

df_positive <- rbind(dfdf1, dfdf2, dfdf3, dfdf4, df_positive)



df_agg <- aggregate(linke$el.joy, list(linke$username, linke$months, linke$count, linke$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "joy"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$joy2 <- scale(df_agg$joy)


fixed1 <- plm(joy ~ treat_indirect, model="within", index = c("name", "date"), 
              effect = "twoways", data = df_agg)


model1 <- coeftest(fixed1, vcov=vcovHC(fixed1,type="HC0",cluster="group"))


dfdf1 <- broom::tidy(model1)
dfdf1$model <- "joy"
dfdf1$term <- "The Left"


df_agg <- aggregate(linke$el.enthusiasm, list(linke$username, linke$months, linke$count, linke$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "enthusiasm"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$enthusiasm2 <- scale(df_agg$enthusiasm)


fixed1 <- plm(enthusiasm ~ treat_indirect, model="within", index = c("name", "date"), 
              effect = "twoways", data = df_agg)


model1 <- coeftest(fixed1, vcov=vcovHC(fixed1,type="HC0",cluster="group"))


dfdf2 <- broom::tidy(model1)
dfdf2$model <- "enthusiasm"
dfdf2$term <- "The Left"

df_agg <- aggregate(linke$el.pride, list(linke$username, linke$months, linke$count, linke$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "pride"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$pride2 <- scale(df_agg$pride)


fixed1 <- plm(pride ~ treat_indirect, model="within", index = c("name", "date"), 
              effect = "twoways", data = df_agg)


model1 <- coeftest(fixed1, vcov=vcovHC(fixed1,type="HC0",cluster="group"))


dfdf3 <- broom::tidy(model1)
dfdf3$model <- "pride"
dfdf3$term <- "The Left"


df_agg <- aggregate(linke$el.hope, list(linke$username, linke$months, linke$count, linke$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "hope"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$hope2 <- scale(df_agg$hope)


fixed1 <- plm(hope ~ treat_indirect, model="within", index = c("name", "date"), 
              effect = "twoways", data = df_agg)


model1 <- coeftest(fixed1, vcov=vcovHC(fixed1,type="HC0",cluster="group"))


dfdf4 <- broom::tidy(model1)
dfdf4$model <- "hope"
dfdf4$term <- "The Left"

df_positive <- rbind(dfdf1, dfdf2, dfdf3, dfdf4, df_positive)






df_agg <- aggregate(cdu$el.joy, list(cdu$username, cdu$months, cdu$count, cdu$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "joy"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$joy2 <- scale(df_agg$joy)


fixed1 <- plm(joy ~ treat_indirect, model="within", index = c("name", "date"), 
              effect = "twoways", data = df_agg)


model1 <- coeftest(fixed1, vcov=vcovHC(fixed1,type="HC0",cluster="group"))


dfdf1 <- broom::tidy(model1)
dfdf1$model <- "joy"
dfdf1$term <- "CDU/CSU"


df_agg <- aggregate(cdu$el.enthusiasm, list(cdu$username, cdu$months, cdu$count, cdu$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "enthusiasm"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$enthusiasm2 <- scale(df_agg$enthusiasm)


fixed1 <- plm(enthusiasm ~ treat_indirect, model="within", index = c("name", "date"), 
              effect = "twoways", data = df_agg)


model1 <- coeftest(fixed1, vcov=vcovHC(fixed1,type="HC0",cluster="group"))


dfdf2 <- broom::tidy(model1)
dfdf2$model <- "enthusiasm"
dfdf2$term <- "CDU/CSU"

df_agg <- aggregate(cdu$el.pride, list(cdu$username, cdu$months, cdu$count, cdu$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "pride"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$pride2 <- scale(df_agg$pride)


fixed1 <- plm(pride ~ treat_indirect, model="within", index = c("name", "date"), 
              effect = "twoways", data = df_agg)


model1 <- coeftest(fixed1, vcov=vcovHC(fixed1,type="HC0",cluster="group"))


dfdf3 <- broom::tidy(model1)
dfdf3$model <- "pride"
dfdf3$term <- "CDU/CSU"


df_agg <- aggregate(cdu$el.hope, list(cdu$username, cdu$months, cdu$count, cdu$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "hope"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$hope2 <- scale(df_agg$hope)


fixed1 <- plm(hope ~ treat_indirect, model="within", index = c("name", "date"), 
              effect = "twoways", data = df_agg)


model1 <- coeftest(fixed1, vcov=vcovHC(fixed1,type="HC0",cluster="group"))


dfdf4 <- broom::tidy(model1)
dfdf4$model <- "hope"
dfdf4$term <- "CDU/CSU"

df_positive <- rbind(dfdf1, dfdf2, dfdf3, dfdf4, df_positive)





df_agg <- aggregate(fdp$el.joy, list(fdp$username, fdp$months, fdp$count, fdp$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "joy"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$joy2 <- scale(df_agg$joy)


fixed1 <- plm(joy ~ treat_indirect, model="within", index = c("name", "date"), 
              effect = "twoways", data = df_agg)


model1 <- coeftest(fixed1, vcov=vcovHC(fixed1,type="HC0",cluster="group"))


dfdf1 <- broom::tidy(model1)
dfdf1$model <- "joy"
dfdf1$term <- "FDP"


df_agg <- aggregate(fdp$el.enthusiasm, list(fdp$username, fdp$months, fdp$count, fdp$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "enthusiasm"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$enthusiasm2 <- scale(df_agg$enthusiasm)


fixed1 <- plm(enthusiasm ~ treat_indirect, model="within", index = c("name", "date"), 
              effect = "twoways", data = df_agg)


model1 <- coeftest(fixed1, vcov=vcovHC(fixed1,type="HC0",cluster="group"))


dfdf2 <- broom::tidy(model1)
dfdf2$model <- "enthusiasm"
dfdf2$term <- "FDP"

df_agg <- aggregate(fdp$el.pride, list(fdp$username, fdp$months, fdp$count, fdp$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "pride"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$pride2 <- scale(df_agg$pride)


fixed1 <- plm(pride ~ treat_indirect, model="within", index = c("name", "date"), 
              effect = "twoways", data = df_agg)


model1 <- coeftest(fixed1, vcov=vcovHC(fixed1,type="HC0",cluster="group"))


dfdf3 <- broom::tidy(model1)
dfdf3$model <- "pride"
dfdf3$term <- "FDP"


df_agg <- aggregate(fdp$el.hope, list(fdp$username, fdp$months, fdp$count, fdp$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "hope"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$hope2 <- scale(df_agg$hope)


fixed1 <- plm(hope ~ treat_indirect, model="within", index = c("name", "date"), 
              effect = "twoways", data = df_agg)


model1 <- coeftest(fixed1, vcov=vcovHC(fixed1,type="HC0",cluster="group"))


dfdf4 <- broom::tidy(model1)
dfdf4$model <- "hope"
dfdf4$term <- "FDP"

df_positive <- rbind(dfdf1, dfdf2, dfdf3, dfdf4, df_positive)



df_agg <- aggregate(afd$el.joy, list(afd$username, afd$months, afd$count, afd$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "joy"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$joy2 <- scale(df_agg$joy)


fixed1 <- plm(joy ~ treat_indirect, model="within", index = c("name", "date"), 
              effect = "twoways", data = df_agg)


model1 <- coeftest(fixed1, vcov=vcovHC(fixed1,type="HC0",cluster="group"))


dfdf1 <- broom::tidy(model1)
dfdf1$model <- "joy"
dfdf1$term <- "AfD"


df_agg <- aggregate(afd$el.enthusiasm, list(afd$username, afd$months, afd$count, afd$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "enthusiasm"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$enthusiasm2 <- scale(df_agg$enthusiasm)


fixed1 <- plm(enthusiasm ~ treat_indirect, model="within", index = c("name", "date"), 
              effect = "twoways", data = df_agg)


model1 <- coeftest(fixed1, vcov=vcovHC(fixed1,type="HC0",cluster="group"))


dfdf2 <- broom::tidy(model1)
dfdf2$model <- "enthusiasm"
dfdf2$term <- "AfD"


df_agg <- aggregate(afd$el.pride, list(afd$username, afd$months, afd$count, afd$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "pride"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$pride2 <- scale(df_agg$pride)


fixed1 <- plm(pride ~ treat_indirect, model="within", index = c("name", "date"), 
              effect = "twoways", data = df_agg)


model1 <- coeftest(fixed1, vcov=vcovHC(fixed1,type="HC0",cluster="group"))


dfdf3 <- broom::tidy(model1)
dfdf3$model <- "pride"
dfdf3$term <- "AfD"


df_agg <- aggregate(afd$el.hope, list(afd$username, afd$months, afd$count, afd$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "hope"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$hope2 <- scale(df_agg$hope)


fixed1 <- plm(hope ~ treat_indirect, model="within", index = c("name", "date"), 
              effect = "twoways", data = df_agg)


model1 <- coeftest(fixed1, vcov=vcovHC(fixed1,type="HC0",cluster="group"))


dfdf4 <- broom::tidy(model1)
dfdf4$model <- "hope"
dfdf4$term <- "AfD"

df_positive <- rbind(dfdf1, dfdf2, dfdf3, dfdf4, df_positive)




df_agg <- aggregate(green$el.joy, list(green$username, green$months, green$count, green$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "joy"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$joy2 <- scale(df_agg$joy)


fixed1 <- plm(joy ~ treat_indirect, model="within", index = c("name", "date"), 
              effect = "twoways", data = df_agg)


model1 <- coeftest(fixed1, vcov=vcovHC(fixed1,type="HC0",cluster="group"))


dfdf1 <- broom::tidy(model1)
dfdf1$model <- "joy"
dfdf1$term <- "Greens"


df_agg <- aggregate(green$el.enthusiasm, list(green$username, green$months, green$count, green$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "enthusiasm"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$enthusiasm2 <- scale(df_agg$enthusiasm)


fixed1 <- plm(enthusiasm ~ treat_indirect, model="within", index = c("name", "date"), 
              effect = "twoways", data = df_agg)


model1 <- coeftest(fixed1, vcov=vcovHC(fixed1,type="HC0",cluster="group"))


dfdf2 <- broom::tidy(model1)
dfdf2$model <- "enthusiasm"
dfdf2$term <- "Greens"

df_agg <- aggregate(green$el.pride, list(green$username, green$months, green$count, green$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "pride"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$pride2 <- scale(df_agg$pride)


fixed1 <- plm(pride ~ treat_indirect, model="within", index = c("name", "date"), 
              effect = "twoways", data = df_agg)


model1 <- coeftest(fixed1, vcov=vcovHC(fixed1,type="HC0",cluster="group"))


dfdf3 <- broom::tidy(model1)
dfdf3$model <- "pride"
dfdf3$term <- "Greens"


df_agg <- aggregate(green$el.hope, list(green$username, green$months, green$count, green$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "hope"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$hope2 <- scale(df_agg$hope)


fixed1 <- plm(hope ~ treat_indirect, model="within", index = c("name", "date"), 
              effect = "twoways", data = df_agg)


model1 <- coeftest(fixed1, vcov=vcovHC(fixed1,type="HC0",cluster="group"))


dfdf4 <- broom::tidy(model1)
dfdf4$model <- "hope"
dfdf4$term <- "Greens"

df_positive <- rbind(dfdf1, dfdf2, dfdf3, dfdf4, df_positive)


df_positive$term <- factor(df_positive$term, levels=c("The Left", "Greens", "SPD", "CDU/CSU", "FDP", "AfD"))


dwplot(df_positive, show_intercept = TRUE, ci = .95,
       vline = geom_vline(xintercept = 0, colour = "grey60", linetype = 2)) %>%
  relabel_predictors(c(fdp = "FDP", 
                       cdu = "CDU/CSU", 
                       linke = "The Left", 
                       spd = "SPD",
                       afd = "AfD",
                       green = "The Greens")) +
  xlab("Coefficient Estimate") + ylab("") +
  scale_colour_manual(name = "Emotion",
                      labels = c("Joy", "Enthusiasm", "Pride", "Hope"),
                      values = c("#6699cc", "#CC6666", "#66CC99", "#FFA38C")) +
  theme_bw()

# Exporting figure to .pdf
ggsave("./figures/figureD3.pdf")


#### Figure E1 ###############################################################
# Use the ws_parl dataframe as a starting point for analysis.
df_analysis <- ws_parl

# Convert 'date2' to a numeric format, subtract a constant (17434), and store the result in a new column 'date3'.
df_analysis$date3 <- as.numeric(df_analysis$date2) - 17434

# Initialize counters.
k <- 1
j <- 0

# Initialize a new column 'months' with NULL values in the 'df_analysis' dataframe.
df_analysis$months <- NULL

# Segment the 'date3' column into 40 intervals (or 'months') of 30 units each.
for (i in seq(from = 30, to = 1200, by = 30)) {
  df_analysis$months[df_analysis$date3 < i & df_analysis$date3 > j] <- k
  j <- i
  k <- k + 1
}

# Convert the 'date' column to a standard YYYY-MM-01 format.
df_analysis$date3 <- format(df_analysis$date, "%Y-%m-01")

# Filter out rows where 'wkindirekt' is NA.
df_analysis2 <- df_analysis[!is.na(df_analysis$wkindirekt),]

# Filter out rows related to topics 56 and 16 from df_analysis2.
df3 <- df_analysis2[df_analysis2$topic == 56 | df_analysis2$topic == 16,]

# Clean up the party names by removing spaces and Unicode character property escapes.
df_analysis2$party <- gsub(" ", "", df_analysis2$party)
df_analysis2$party <- gsub("(*UCP)\\s*", "", df_analysis2$party, perl = TRUE)

# Create party-specific dataframes based on filtered conditions.
afd <- df3[!(df3$party != "AfD" & df3$treat_indirect == 1),]
green <- df3[!(df3$party!="Greens" & df3$treat_indirect==1),]
spd <- df3[!(df3$party!="SPD" & df3$treat_indirect==1),]
linke <- df3[!(df3$party!="The Left" & df3$treat_indirect==1),]
cdu <- df3[!(df3$party!="CDU/CSU" & df3$treat_indirect==1),]
fdp <- df3[!(df3$party!="FDP" & df3$treat_indirect==1),]

# Aggregate the disgust scores (el.disgust) for the party 'AfD' based on name, months, treat_indirect, and party.
# Compute the mean disgust score for each group.
df_afd <- aggregate(afd$el.disgust, list(afd$name, afd$months, afd$treat_indirect, afd$party), mean)
colnames(df_afd)[1] <- "name"
colnames(df_afd)[2] <- "date"
colnames(df_afd)[3] <- "treat_indirect"
colnames(df_afd)[4] <- "party"
colnames(df_afd)[5] <- "disgust"


# Sort the dataframe by name and treat_indirect.
df_afd <- df_afd[order(df_afd$name, -abs(df_afd$treat_indirect)),]
# Remove duplicate rows based on name and date.
df_afd <- df_afd %>% distinct(name, date, .keep_all = T)

# For each individual (name), identify the earliest month where treat_indirect is 1.
earliest_month <- df_afd %>%
  group_by(name) %>%
  filter(treat_indirect == 1) %>%
  mutate(earliest_month = min(date))

# Extract unique individual names.
pol_names <- earliest_month$name
pol_names <- unique(pol_names)

# Initialize a new column 'treatment_month' with NA values.
df_afd$treatment_month <- NA

# Populate the 'treatment_month' column with the earliest treatment month for each individual.
for (i in pol_names) {
  df_afd$treatment_month[df_afd$name == i] <- earliest_month$earliest_month[earliest_month$name == i]
}

# Convert the df_afd dataframe to a data.table format for efficient manipulation.
dt1 <- setDT(df_afd)

# Calculate the time difference between each date and the treatment month for each individual.
dt1$time_to_treat <- dt1$date - dt1$treatment_month
dt1$time_to_treat[is.na(dt1$time_to_treat)] <- 0
dt1$treatment_month[is.na(dt1$treatment_month)] <- 10000

# Estimate a fixed effects regression model with 'disgust' as the dependent variable.
# The model includes a key interaction term (time × treatment status), individual and time fixed effects, and clustered standard errors.
mod_sa = feols(disgust ~ sunab(treatment_month, date) +   ## key interaction: time × treatment status
                 name + date,                             ## FEs
               cluster = ~ name,                          ## Clustered SEs
               data = dt1)

pdf("./figures/figureE1.pdf")

# Plot the regression results, emphasizing the effect of time to treatment on the outcome 'disgust' for the party 'AfD'.
iplot(mod_sa, ref.line = 0,
      xlab = 'Time to treatment',
      main = "Effect on Disgust (AfD)")

dev.off()


#### Figure E2 ###################################################################


df_analysis <- ws_parl


df_analysis$date3 <- as.numeric(df_analysis$date2)-17434
k <- 1
j <- 0
df_analysis$months <- NULL
for (i in seq(from=30, to=1200, by=30)){
  df_analysis$months[df_analysis$date3<i & df_analysis$date3>j] <- k
  j <- i
  k <- k + 1
}

df_analysis$date3 <- format(df_analysis$date,"%Y-%m-01")

df_analysis2 <- df_analysis[!is.na(df_analysis$wkindirekt),]


df3 <- df_analysis2[df_analysis2$topic==56 | df_analysis2$topic==16,]

df_analysis2$party <- gsub(" ", "", df_analysis2$party)
df_analysis2$party <- gsub("(*UCP)\\s*", "", df_analysis2$party, perl = TRUE)

afd <- df3[!(df3$party!="AfD" & df3$treat_indirect==1),]
green <- df3[!(df3$party!="Greens" & df3$treat_indirect==1),]
spd <- df3[!(df3$party!="SPD" & df3$treat_indirect==1),]
linke <- df3[!(df3$party!="The Left" & df3$treat_indirect==1),]
cdu <- df3[!(df3$party!="CDU/CSU" & df3$treat_indirect==1),]
fdp <- df3[!(df3$party!="FDP" & df3$treat_indirect==1),]

df_fdp <- aggregate(fdp$el.disgust, list(fdp$name, fdp$months, fdp$treat_indirect, fdp$party), mean)
colnames(df_fdp)[1] <- "name"
colnames(df_fdp)[2] <- "date"
colnames(df_fdp)[3] <- "treat_indirect"
colnames(df_fdp)[4] <- "party"
colnames(df_fdp)[5] <- "disgust"

df_fdp <- df_fdp[order(df_fdp$name, -abs(df_fdp$treat_indirect) ), ] ### sort first
df_fdp <- df_fdp %>% distinct(name, date, .keep_all = T)

earliest_month <- df_fdp %>%
  group_by(name) %>%
  filter(treat_indirect==1) %>%
  mutate(earliest_month = min(date))

pol_names <- earliest_month$name
pol_names <- unique(pol_names)

df_fdp$treatment_month <- NA
for (i in pol_names) {
  df_fdp$treatment_month[df_fdp$name==i] <- earliest_month$earliest_month[earliest_month$name==i]
}

dt1 <- setDT(df_fdp)

dt1$time_to_treat <- dt1$date-dt1$treatment_month
dt1$time_to_treat[is.na(dt1$time_to_treat)] <- 0

dt1$treatment_month[is.na(dt1$treatment_month)] <- 10000


mod_sa = feols(disgust ~ sunab(treatment_month, date) +   ## key interaction: time × treatment status
                 name + date,                             ## FEs
               cluster = ~ name,                          ## Clustered SEs
               data = dt1)

pdf("./figures/figureE2.pdf")

iplot(mod_sa, ref.line = 0,
      xlab = 'Time to treatment',
      main = 'Effect on Disgust (FDP)')

dev.off()

#### Figure F1 ################################################################

df_analysis <- ws_parl

df_analysis$direct_elect <- 0
df_analysis$direct_elect[df_analysis$wkdirekt>0] <- 1


df_analysis$date3 <- as.numeric(df_analysis$date2)-17434
k <- 1
j <- 0
df_analysis$months <- NULL
for (i in seq(from=30, to=1200, by=30)){
  df_analysis$months[df_analysis$date3<i & df_analysis$date3>j] <- k
  j <- i
  k <- k + 1
}

df_analysis$date3 <- format(df_analysis$date,"%Y-%m-01")

df_analysis2 <- df_analysis[!is.na(df_analysis$wkindirekt),]

df3 <- df_analysis2[df_analysis2$topic==56 | df_analysis2$topic==16,]

df_analysis2$party <- gsub(" ", "", df_analysis2$party)
df_analysis2$party <- gsub("(*UCP)\\s*", "", df_analysis2$party, perl = TRUE)

afd <- df3[!(df3$party!="AfD" & df3$treat_indirect==1),]
green <- df3[!(df3$party!="Greens" & df3$treat_indirect==1),]
spd <- df3[!(df3$party!="SPD" & df3$treat_indirect==1),]
linke <- df3[!(df3$party!="The Left" & df3$treat_indirect==1),]
cdu <- df3[!(df3$party!="CDU/CSU" & df3$treat_indirect==1),]
fdp <- df3[!(df3$party!="FDP" & df3$treat_indirect==1),]


df_agg <- aggregate(afd$el.disgust, list(afd$name, afd$months, afd$count, afd$party, afd$direct_elect), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "direct_elect"
colnames(df_agg)[6] <- "disgust"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$disgust2 <- scale(df_agg$disgust)

fixed4 <- plm(disgust ~ treat_indirect*direct_elect, model="within", index = c("name", "date"), 
              effect = "twoways", data = df_agg)
model4 <- coeftest(fixed4, vcov=vcovHC(fixed4,type="HC0",cluster="group"))


dfdf4 <- broom::tidy(model4)

dfdf4$term[1] <- "Main Effect"
dfdf4$term[2] <- "Interaction Effect (directly elected)"

dwplot(dfdf4, show_intercept = TRUE, ci = .95,
       vline = geom_vline(xintercept = 0, colour = "grey60", linetype = 2)) +
  theme_bw()

# Exporting figure to .pdf
ggsave("./figures/figureF1.pdf")


#### Figure F2 ##################################################################


df_analysis <- ws_parl

df_analysis$direct_elect <- 0
df_analysis$direct_elect[df_analysis$wkdirekt>0] <- 1


df_analysis$date3 <- as.numeric(df_analysis$date2)-17434
k <- 1
j <- 0
df_analysis$months <- NULL
for (i in seq(from=30, to=1200, by=30)){
  df_analysis$months[df_analysis$date3<i & df_analysis$date3>j] <- k
  j <- i
  k <- k + 1
}

df_analysis$date3 <- format(df_analysis$date,"%Y-%m-01")

df_analysis2 <- df_analysis[!is.na(df_analysis$wkindirekt),]

df3 <- df_analysis2[df_analysis2$topic==56 | df_analysis2$topic==16,]

df_analysis2$party <- gsub(" ", "", df_analysis2$party)
df_analysis2$party <- gsub("(*UCP)\\s*", "", df_analysis2$party, perl = TRUE)

afd <- df3[!(df3$party!="AfD" & df3$treat_indirect==1),]
green <- df3[!(df3$party!="Greens" & df3$treat_indirect==1),]
spd <- df3[!(df3$party!="SPD" & df3$treat_indirect==1),]
linke <- df3[!(df3$party!="The Left" & df3$treat_indirect==1),]
cdu <- df3[!(df3$party!="CDU/CSU" & df3$treat_indirect==1),]
fdp <- df3[!(df3$party!="FDP" & df3$treat_indirect==1),]


df_agg <- aggregate(afd$el.anger, list(afd$name, afd$months, afd$count, afd$party, afd$direct_elect), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "direct_elect"
colnames(df_agg)[6] <- "anger"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$anger2 <- scale(df_agg$anger)

fixed3 <- plm(anger ~ treat_indirect*direct_elect, model="within", index = c("name", "date"), 
              effect = "twoways", data = df_agg)

model3 <- coeftest(fixed3, vcov=vcovHC(fixed3,type="HC0",cluster="group"))

dfdf3 <- broom::tidy(model3)

dfdf3$term[1] <- "Main Effect"
dfdf3$term[2] <- "Interaction Effect (directly elected)"

dwplot(dfdf3, show_intercept = TRUE, ci = .95,
       vline = geom_vline(xintercept = 0, colour = "grey60", linetype = 2)) +
  theme_bw()

# Exporting figure to .pdf
ggsave("./figures/figureF2.pdf")

#### Figure G1 ###################################################################

df_analysis <- ws_parl


df_analysis$date3 <- as.numeric(df_analysis$date2)-17434
k <- 1
j <- 0
df_analysis$months <- NULL
for (i in seq(from=30, to=1200, by=30)){
  df_analysis$months[df_analysis$date3<i & df_analysis$date3>j] <- k
  j <- i
  k <- k + 1
}

df_analysis$date3 <- format(df_analysis$date,"%Y-%m-01")

df_analysis2 <- df_analysis[!is.na(df_analysis$wkindirekt),]

df5 <- df_analysis2[df_analysis2$topic!=56 & df_analysis2$topic!=16,]


df_analysis2$party <- gsub(" ", "", df_analysis2$party)
df_analysis2$party <- gsub("(*UCP)\\s*", "", df_analysis2$party, perl = TRUE)

afd <- df5[!(df5$party!="AfD" & df5$treat_indirect==1),]
green <- df5[!(df5$party!="Greens" & df5$treat_indirect==1),]
spd <- df5[!(df5$party!="SPD" & df5$treat_indirect==1),]
linke <- df5[!(df5$party!="The Left" & df5$treat_indirect==1),]
cdu <- df5[!(df5$party!="CDU/CSU" & df5$treat_indirect==1),]
fdp <- df5[!(df5$party!="FDP" & df5$treat_indirect==1),]

df_agg <- aggregate(green$el.anger, list(green$name, green$months, green$count, green$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "anger"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$anger2 <- scale(df_agg$anger)

fixed1 <- plm(anger ~ treat_indirect, model="within", index = c("name", "date"), 
              effect = "twoways", data = df_agg)
model1 <- coeftest(fixed1, vcov=vcovHC(fixed1,type="HC0",cluster="group"))

dfdf1 <- broom::tidy(model1)
dfdf1$model <- "Anger"
dfdf1$term <- "Greens"

df_agg <- aggregate(green$el.disgust, list(green$name, green$months, green$count, green$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "disgust"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$disgust2 <- scale(df_agg$disgust)

fixed2 <- plm(disgust ~ treat_indirect, model="within", index = c("name", "date"), 
              effect = "twoways", data = df_agg)
model2 <- coeftest(fixed2, vcov=vcovHC(fixed2,type="HC0",cluster="group"))

dfdf2 <- broom::tidy(model2)
dfdf2$model <- "Disgust"
dfdf2$term <- "Greens"



df_agg <- aggregate(afd$el.anger, list(afd$name, afd$months, afd$count, afd$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "anger"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$anger2 <- scale(df_agg$anger)

fixed3 <- plm(anger ~ treat_indirect, model="within", index = c("name", "date"), 
              effect = "twoways", data = df_agg)
model3 <- coeftest(fixed3, vcov=vcovHC(fixed3,type="HC0",cluster="group"))

dfdf3 <- broom::tidy(model3)
dfdf3$model <- "Anger"
dfdf3$term <- "AfD"

df_agg <- aggregate(afd$el.disgust, list(afd$name, afd$months, afd$count, afd$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "disgust"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$disgust2 <- scale(df_agg$disgust)

fixed4 <- plm(disgust ~ treat_indirect, model="within", index = c("name", "date"), 
              effect = "twoways", data = df_agg)
model4 <- coeftest(fixed4, vcov=vcovHC(fixed4,type="HC0",cluster="group"))

dfdf4 <- broom::tidy(model4)
dfdf4$model <- "Disgust"
dfdf4$term <- "AfD"



df_agg <- aggregate(cdu$el.anger, list(cdu$name, cdu$months, cdu$count, cdu$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "anger"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$anger2 <- scale(df_agg$anger)

fixed5 <- plm(anger ~ treat_indirect, model="within", index = c("name", "date"), 
              effect = "twoways", data = df_agg)
model5 <- coeftest(fixed5, vcov=vcovHC(fixed5,type="HC0",cluster="group"))

dfdf5 <- broom::tidy(model5)
dfdf5$model <- "Anger"
dfdf5$term <- "CDU/CSU"

df_agg <- aggregate(cdu$el.disgust, list(cdu$name, cdu$months, cdu$count, cdu$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "disgust"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$disgust2 <- scale(df_agg$disgust)

fixed6 <- plm(disgust ~ treat_indirect, model="within", index = c("name", "date"), 
              effect = "twoways", data = df_agg)
model6 <- coeftest(fixed6, vcov=vcovHC(fixed6,type="HC0",cluster="group"))

dfdf6 <- broom::tidy(model6)
dfdf6$model <- "Disgust"
dfdf6$term <- "CDU/CSU"



df_agg <- aggregate(fdp$el.anger, list(fdp$name, fdp$months, fdp$count, fdp$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "anger"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$anger2 <- scale(df_agg$anger)

fixed5 <- plm(anger ~ treat_indirect, model="within", index = c("name", "date"), 
              effect = "twoways", data = df_agg)
model5 <- coeftest(fixed5, vcov=vcovHC(fixed5,type="HC0",cluster="group"))

dfdf7 <- broom::tidy(model5)
dfdf7$model <- "Anger"
dfdf7$term <- "FDP"

df_agg <- aggregate(fdp$el.disgust, list(fdp$name, fdp$months, fdp$count, fdp$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "disgust"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$disgust2 <- scale(df_agg$disgust)

fixed6 <- plm(disgust ~ treat_indirect, model="within", index = c("name", "date"), 
              effect = "twoways", data = df_agg)
model6 <- coeftest(fixed6, vcov=vcovHC(fixed6,type="HC0",cluster="group"))

dfdf8 <- broom::tidy(model6)
dfdf8$model <- "Disgust"
dfdf8$term <- "FDP"

df_agg <- aggregate(spd$el.anger, list(spd$name, spd$months, spd$count, spd$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "anger"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$anger2 <- scale(df_agg$anger)

fixed5 <- plm(anger ~ treat_indirect, model="within", index = c("name", "date"), 
              effect = "twoways", data = df_agg)
model5 <- coeftest(fixed5, vcov=vcovHC(fixed5,type="HC0",cluster="group"))

dfdf9 <- broom::tidy(model5)
dfdf9$model <- "Anger"
dfdf9$term <- "SPD"

df_agg <- aggregate(spd$el.disgust, list(spd$name, spd$months, spd$count, spd$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "disgust"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$disgust2 <- scale(df_agg$disgust)

fixed6 <- plm(disgust ~ treat_indirect, model="within", index = c("name", "date"), 
              effect = "twoways", data = df_agg)
model6 <- coeftest(fixed6, vcov=vcovHC(fixed6,type="HC0",cluster="group"))

dfdf10 <- broom::tidy(model6)
dfdf10$model <- "Disgust"
dfdf10$term <- "SPD"


df_agg <- aggregate(linke$el.anger, list(linke$name, linke$months, linke$count, linke$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "anger"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$anger2 <- scale(df_agg$anger)

fixed5 <- plm(anger ~ treat_indirect, model="within", index = c("name", "date"), 
              effect = "twoways", data = df_agg)
model5 <- coeftest(fixed5, vcov=vcovHC(fixed5,type="HC0",cluster="group"))

dfdf11 <- broom::tidy(model5)
dfdf11$model <- "Anger"
dfdf11$term <- "The Left"

df_agg <- aggregate(linke$el.disgust, list(linke$name, linke$months, linke$count, linke$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "disgust"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$disgust2 <- scale(df_agg$disgust)

fixed6 <- plm(disgust ~ treat_indirect, model="within", index = c("name", "date"), 
              effect = "twoways", data = df_agg)
model6 <- coeftest(fixed6, vcov=vcovHC(fixed6,type="HC0",cluster="group"))

dfdf12 <- broom::tidy(model6)
dfdf12$model <- "Disgust"
dfdf12$term <- "The Left"



df_anger <- rbind(dfdf1, dfdf3, dfdf5, dfdf7, dfdf9, dfdf11)

df_disgust <- rbind(dfdf2, dfdf4, dfdf6, dfdf8, dfdf10, dfdf12)

df_anger$term <- factor(df_anger$term, levels=c("The Left", "Greens", "SPD", "CDU/CSU", "FDP", "AfD"))

df_disgust$term <- factor(df_disgust$term, levels=c("The Left", "Greens", "SPD", "CDU/CSU", "FDP", "AfD"))


anger_coef <- dwplot(df_anger, show_intercept = TRUE, ci = .95,
                     vline = geom_vline(xintercept = 0, colour = "grey60", linetype = 2)) %>%
  relabel_predictors(c(fdp = "FDP", 
                       cdu = "CDU/CSU", 
                       linke = "Die Linke", 
                       spd = "SPD")) +
  xlab("Coefficient Estimate (Anger)") + ylab("") +
  scale_colour_manual(values = c("#6699cc")) +
  guides(colour = "none") +
  theme_bw()

disgust_coef <- dwplot(df_disgust, show_intercept = TRUE, ci = .95,
                       vline = geom_vline(xintercept = 0, colour = "grey60", linetype = 2)) %>%
  relabel_predictors(c(fdp = "FDP", 
                       cdu = "CDU/CSU", 
                       linke = "Die Linke", 
                       spd = "SPD")) +
  xlab("Coefficient Estimate (Disgust)") + ylab("") +
  scale_colour_manual(values = c("#CC6666")) +
  guides(colour = "none") +
  theme_bw()

ggpubr::ggarrange(anger_coef, disgust_coef)

# Exporting figure to .pdf
ggsave("./figures/figureG1.pdf")

#### Figure H1 ####################################################################

##### Prepare Approval Data Data ##################################################
# Load data with approval dates of wind turbines

test2 <- wind_data_approval %>% 
  group_by(wahlkreis) %>%
  filter(date == min(date))

test2$plz <- NULL
test2 <- unique(test2[,1:3])

# Assign treatment variable based on approval date
ws_parl$treat_approval <- 0
for (i in 1:length(test2$wahlkreis)){
  ws_parl$treat_approval[ws_parl$wkindirekt==test2$wahlkreis[i] & ws_parl$date>=test2$date[i]] <- 1
}

# count_approval variable

ws_parl$count_approval <- 0
df_cap <- wind_data_approval

df_cap$capacity <- df_cap$date <- df_cap$plz <- df_cap$date2 <- df_cap$date <- NULL
df_cap$former_cap <- 0

df_cap <- unique(df_cap[,1:2])

for (i in 1:length(wind_data_approval$wahlkreis)){
  ws_parl$count_approval[ws_parl$wkindirekt==wind_data_approval$wahlkreis[i] & ws_parl$date>=wind_data_approval$date[i]] <- sum(df_cap$former_cap[df_cap$wahlkreis==wind_data_approval$wahlkreis[i]][1], 1)
  df_cap$former_cap[df_cap$wahlkreis==wind_data_approval$wahlkreis[i]] <- sum(df_cap$former_cap[df_cap$wahlkreis==wind_data_approval$wahlkreis[i]][1], 1)
}

##### Analysis ################################################################


df_analysis <- ws_parl


df_analysis$date3 <- as.numeric(df_analysis$date2)-17434
k <- 1
j <- 0
df_analysis$months <- NULL
for (i in seq(from=30, to=1200, by=30)){
  df_analysis$months[df_analysis$date3<i & df_analysis$date3>j] <- k
  j <- i
  k <- k + 1
}

df_analysis$date3 <- format(df_analysis$date,"%Y-%m-01")

df_analysis2 <- df_analysis[!is.na(df_analysis$wkindirekt),]


df3 <- df_analysis2[df_analysis2$topic==56 | df_analysis2$topic==16,]


df_analysis2$party <- gsub(" ", "", df_analysis2$party)
df_analysis2$party <- gsub("(*UCP)\\s*", "", df_analysis2$party, perl = TRUE)

afd <- df3[!(df3$party!="AfD" & df3$treat_indirect==1),]
green <- df3[!(df3$party!="GRUENE" & df3$treat_indirect==1),]
spd <- df3[!(df3$party!="SPD" & df3$treat_indirect==1),]
linke <- df3[!(df3$party!="PDS/LINKE" & df3$treat_indirect==1),]
cdu <- df3[!(df3$party!="CDU/CSU" & df3$treat_indirect==1),]
fdp <- df3[!(df3$party!="FDP" & df3$treat_indirect==1),]


df_agg <- aggregate(green$el.anger, list(green$name, green$months, green$count_approval, green$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "count_approval"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "anger"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$count_approval) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$anger2 <- scale(df_agg$anger)

fixed1 <- plm(anger ~ count_approval, model="within", index = c("name", "date"), 
              effect = "twoways", data = df_agg)
model1 <- coeftest(fixed1, vcov=vcovHC(fixed1,type="HC0",cluster="group"))

dfdf1 <- broom::tidy(model1)
dfdf1$model <- "Anger"
dfdf1$term <- "Greens"

df_agg <- aggregate(green$el.disgust, list(green$name, green$months, green$count_approval, green$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "count_approval"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "disgust"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$count_approval) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$disgust2 <- scale(df_agg$disgust)

fixed2 <- plm(disgust ~ count_approval, model="within", index = c("name", "date"), 
              effect = "twoways", data = df_agg)
model2 <- coeftest(fixed2, vcov=vcovHC(fixed2,type="HC0",cluster="group"))

dfdf2 <- broom::tidy(model2)
dfdf2$model <- "Disgust"
dfdf2$term <- "Greens"

df_agg <- aggregate(afd$el.anger, list(afd$name, afd$months, afd$count_approval, afd$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "count_approval"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "anger"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$count_approval) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$anger2 <- scale(df_agg$anger)

fixed3 <- plm(anger ~ count_approval, model="within", index = c("name", "date"), 
              effect = "twoways", data = df_agg)
model3 <- coeftest(fixed3, vcov=vcovHC(fixed3,type="HC0",cluster="group"))

dfdf3 <- broom::tidy(model3)
dfdf3$model <- "Anger"
dfdf3$term <- "AfD"

df_agg <- aggregate(afd$el.disgust, list(afd$name, afd$months, afd$count_approval, afd$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "count_approval"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "disgust"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$count_approval) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$disgust2 <- scale(df_agg$disgust)

fixed4 <- plm(disgust ~ count_approval, model="within", index = c("name", "date"), 
              effect = "twoways", data = df_agg)
model4 <- coeftest(fixed4, vcov=vcovHC(fixed4,type="HC0",cluster="group"))

dfdf4 <- broom::tidy(model4)
dfdf4$model <- "Disgust"
dfdf4$term <- "AfD"



df_agg <- aggregate(linke$el.anger, list(linke$name, linke$months, linke$count_approval, linke$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "count_approval"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "anger"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$count_approval) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$anger2 <- scale(df_agg$anger)

fixed5 <- plm(anger ~ count_approval, model="within", index = c("name", "date"), 
              effect = "twoways", data = df_agg)
model5 <- coeftest(fixed5, vcov=vcovHC(fixed5,type="HC0",cluster="group"))

dfdf5 <- broom::tidy(model5)
dfdf5$model <- "Anger"
dfdf5$term <- "The Left"

df_agg <- aggregate(linke$el.disgust, list(linke$name, linke$months, linke$count_approval, linke$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "count_approval"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "disgust"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$count_approval) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$disgust2 <- scale(df_agg$disgust)

fixed6 <- plm(disgust ~ count_approval, model="within", index = c("name", "date"), 
              effect = "twoways", data = df_agg)
model6 <- coeftest(fixed6, vcov=vcovHC(fixed6,type="HC0",cluster="group"))

dfdf6 <- broom::tidy(model6)
dfdf6$model <- "Disgust"
dfdf6$term <- "The Left"


df_agg <- aggregate(spd$el.anger, list(spd$name, spd$months, spd$count_approval, spd$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "count_approval"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "anger"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$count_approval) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$anger2 <- scale(df_agg$anger)

fixed7 <- plm(anger ~ count_approval, model="within", index = c("name", "date"), 
              effect = "twoways", data = df_agg)
model7 <- coeftest(fixed7, vcov=vcovHC(fixed7,type="HC0",cluster="group"))

dfdf7 <- broom::tidy(model7)
dfdf7$model <- "Anger"
dfdf7$term <- "SPD"

df_agg <- aggregate(spd$el.disgust, list(spd$name, spd$months, spd$count_approval, spd$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "count_approval"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "disgust"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$count_approval) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$disgust2 <- scale(df_agg$disgust)

fixed8 <- plm(disgust ~ count_approval, model="within", index = c("name", "date"), 
              effect = "twoways", data = df_agg)
model8 <- coeftest(fixed8, vcov=vcovHC(fixed8,type="HC0",cluster="group"))

dfdf8 <- broom::tidy(model8)
dfdf8$model <- "Disgust"
dfdf8$term <- "SPD"


df_agg <- aggregate(fdp$el.anger, list(fdp$name, fdp$months, fdp$count_approval, fdp$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "count_approval"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "anger"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$count_approval) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$anger2 <- scale(df_agg$anger)

fixed9 <- plm(anger ~ count_approval, model="within", index = c("name", "date"), 
              effect = "twoways", data = df_agg)
model9 <- coeftest(fixed9, vcov=vcovHC(fixed9,type="HC0",cluster="group"))

dfdf9 <- broom::tidy(model9)
dfdf9$model <- "Anger"
dfdf9$term <- "FDP"

df_agg <- aggregate(fdp$el.disgust, list(fdp$name, fdp$months, fdp$count_approval, fdp$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "count_approval"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "disgust"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$count_approval) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$disgust2 <- scale(df_agg$disgust)

fixed10 <- plm(disgust ~ count_approval, model="within", index = c("name", "date"), 
               effect = "twoways", data = df_agg)
model10 <- coeftest(fixed10, vcov=vcovHC(fixed10,type="HC0",cluster="group"))

dfdf10 <- broom::tidy(model10)
dfdf10$model <- "Disgust"
dfdf10$term <- "FDP"



df_agg <- aggregate(cdu$el.anger, list(cdu$name, cdu$months, cdu$count_approval, cdu$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "count_approval"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "anger"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$count_approval) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$anger2 <- scale(df_agg$anger)

fixed11 <- plm(anger ~ count_approval, model="within", index = c("name", "date"), 
               effect = "twoways", data = df_agg)
model11 <- coeftest(fixed11, vcov=vcovHC(fixed11,type="HC0",cluster="group"))

dfdf11 <- broom::tidy(model11)
dfdf11$model <- "Anger"
dfdf11$term <- "CDU/CSU"

df_agg <- aggregate(cdu$el.disgust, list(cdu$name, cdu$months, cdu$count_approval, cdu$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "count_approval"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "disgust"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$count_approval) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$disgust2 <- scale(df_agg$disgust)

fixed12 <- plm(disgust ~ count_approval, model="within", index = c("name", "date"), 
               effect = "twoways", data = df_agg)
model12 <- coeftest(fixed12, vcov=vcovHC(fixed12,type="HC0",cluster="group"))

dfdf12 <- broom::tidy(model12)
dfdf12$model <- "Disgust"
dfdf12$term <- "CDU/CSU"




df_rest <- rbind(dfdf1, dfdf2, dfdf3, dfdf4, dfdf5, dfdf6, dfdf7, dfdf8, dfdf9, dfdf10, 
                 dfdf11, dfdf12)

df_rest$term <- factor(df_rest$term, levels=c("The Left", "Greens", "SPD", "CDU/CSU", "FDP", "AfD"))

dwplot(df_rest, show_intercept = TRUE, ci = .95,
       vline = geom_vline(xintercept = 0, colour = "grey60", linetype = 2)) %>%
  relabel_predictors(c(afd = "AfD", 
                       Greens = "The Greens")) +
  xlab("Coefficient Estimate") + ylab("") +
  scale_colour_manual(name = "Emotion",
                      labels = c("Anger", "Disgust"),
                      values = c("#6699cc", "#CC6666")) +
  theme_bw()

# Exporting figure to .pdf
ggsave("./figures/figureH1.pdf")

#### Figure J1 #####################################################################


df_analysis <- ws_parl


df_analysis$date3 <- as.numeric(df_analysis$date2)-17434
k <- 1
j <- 0
df_analysis$months <- NULL
for (i in seq(from=30, to=1200, by=30)){
  df_analysis$months[df_analysis$date3<i & df_analysis$date3>j] <- k
  j <- i
  k <- k + 1
}

df_analysis$date3 <- format(df_analysis$date,"%Y-%m-01")

df_analysis2 <- df_analysis[!is.na(df_analysis$wkindirekt),]

df3 <- df_analysis2[df_analysis2$topic==56 | df_analysis2$topic==16,]

df_analysis2$party <- gsub(" ", "", df_analysis2$party)
df_analysis2$party <- gsub("(*UCP)\\s*", "", df_analysis2$party, perl = TRUE)

afd <- df3[!(df3$party!="AfD" & df3$treat_indirect==1),]
green <- df3[!(df3$party!="Greens" & df3$treat_indirect==1),]
spd <- df3[!(df3$party!="SPD" & df3$treat_indirect==1),]
linke <- df3[!(df3$party!="The Left" & df3$treat_indirect==1),]
cdu <- df3[!(df3$party!="CDU/CSU" & df3$treat_indirect==1),]
fdp <- df3[!(df3$party!="FDP" & df3$treat_indirect==1),]




df_agg <- aggregate(green$purity.norm, list(green$name, green$months, green$count, green$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "purity"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$purity2 <- scale(df_agg$purity)

fixed1 <- plm(purity ~ treat_indirect, model="within", index = c("name", "date"), 
              effect = "twoways", data = df_agg)
model1 <- coeftest(fixed1, vcov=vcovHC(fixed1,type="HC0",cluster="group"))

dfdf1 <- broom::tidy(model1)
dfdf1$model <- "purity"
dfdf1$term <- "Greens"

df_agg <- aggregate(green$fairness_care.norm, list(green$name, green$months, green$count, green$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "care_harm"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$care_harm2 <- scale(df_agg$care_harm)

fixed2 <- plm(care_harm ~ treat_indirect, model="within", index = c("name", "date"), 
              effect = "twoways", data = df_agg)
model2 <- coeftest(fixed2, vcov=vcovHC(fixed2,type="HC0",cluster="group"))

dfdf2 <- broom::tidy(model2)
dfdf2$model <- "care_harm"
dfdf2$term <- "Greens"



df_agg <- aggregate(afd$purity.norm, list(afd$name, afd$months, afd$count, afd$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "purity"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$purity2 <- scale(df_agg$purity)

fixed3 <- plm(purity ~ treat_indirect, model="within", index = c("name", "date"), 
              effect = "twoways", data = df_agg)
model3 <- coeftest(fixed3, vcov=vcovHC(fixed3,type="HC0",cluster="group"))

dfdf3 <- broom::tidy(model3)
dfdf3$model <- "purity"
dfdf3$term <- "AfD"

df_agg <- aggregate(afd$fairness_care.norm, list(afd$name, afd$months, afd$count, afd$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "care_harm"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$care_harm2 <- scale(df_agg$care_harm)

fixed4 <- plm(care_harm ~ treat_indirect, model="within", index = c("name", "date"), 
              effect = "twoways", data = df_agg)
model4 <- coeftest(fixed4, vcov=vcovHC(fixed4,type="HC0",cluster="group"))

dfdf4 <- broom::tidy(model4)
dfdf4$model <- "care_harm"
dfdf4$term <- "AfD"



df_agg <- aggregate(linke$purity.norm, list(linke$name, linke$months, linke$count, linke$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "purity"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$purity2 <- scale(df_agg$purity)

fixed5 <- plm(purity ~ treat_indirect, model="within", index = c("name", "date"), 
              effect = "twoways", data = df_agg)
model5 <- coeftest(fixed5, vcov=vcovHC(fixed5,type="HC0",cluster="group"))

dfdf5 <- broom::tidy(model5)
dfdf5$model <- "purity"
dfdf5$term <- "The Left"

df_agg <- aggregate(linke$fairness_care.norm, list(linke$name, linke$months, linke$count, linke$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "care_harm"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$care_harm2 <- scale(df_agg$care_harm)

fixed6 <- plm(care_harm ~ treat_indirect, model="within", index = c("name", "date"), 
              effect = "twoways", data = df_agg)
model6 <- coeftest(fixed6, vcov=vcovHC(fixed6,type="HC0",cluster="group"))

dfdf6 <- broom::tidy(model6)
dfdf6$model <- "care_harm"
dfdf6$term <- "The Left"


df_agg <- aggregate(spd$purity.norm, list(spd$name, spd$months, spd$count, spd$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "purity"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$purity2 <- scale(df_agg$purity)

fixed7 <- plm(purity ~ treat_indirect, model="within", index = c("name", "date"), 
              effect = "twoways", data = df_agg)
model7 <- coeftest(fixed7, vcov=vcovHC(fixed7,type="HC0",cluster="group"))

dfdf7 <- broom::tidy(model7)
dfdf7$model <- "purity"
dfdf7$term <- "SPD"

df_agg <- aggregate(spd$fairness_care.norm, list(spd$name, spd$months, spd$count, spd$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "care_harm"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$care_harm2 <- scale(df_agg$care_harm)

fixed8 <- plm(care_harm ~ treat_indirect, model="within", index = c("name", "date"), 
              effect = "twoways", data = df_agg)
model8 <- coeftest(fixed8, vcov=vcovHC(fixed8,type="HC0",cluster="group"))

dfdf8 <- broom::tidy(model8)
dfdf8$model <- "care_harm"
dfdf8$term <- "SPD"


df_agg <- aggregate(fdp$purity.norm, list(fdp$name, fdp$months, fdp$count, fdp$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "purity"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$purity2 <- scale(df_agg$purity)

fixed9 <- plm(purity ~ treat_indirect, model="within", index = c("name", "date"), 
              effect = "twoways", data = df_agg)
model9 <- coeftest(fixed9, vcov=vcovHC(fixed9,type="HC0",cluster="group"))

dfdf9 <- broom::tidy(model9)
dfdf9$model <- "purity"
dfdf9$term <- "FDP"

df_agg <- aggregate(fdp$fairness_care.norm, list(fdp$name, fdp$months, fdp$count, fdp$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "care_harm"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$care_harm2 <- scale(df_agg$care_harm)

fixed10 <- plm(care_harm ~ treat_indirect, model="within", index = c("name", "date"), 
               effect = "twoways", data = df_agg)
model10 <- coeftest(fixed10, vcov=vcovHC(fixed10,type="HC0",cluster="group"))

dfdf10 <- broom::tidy(model10)
dfdf10$model <- "care_harm"
dfdf10$term <- "FDP"



df_agg <- aggregate(cdu$purity.norm, list(cdu$name, cdu$months, cdu$count, cdu$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "purity"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$purity2 <- scale(df_agg$purity)

fixed11 <- plm(purity ~ treat_indirect, model="within", index = c("name", "date"), 
               effect = "twoways", data = df_agg)
model11 <- coeftest(fixed11, vcov=vcovHC(fixed11,type="HC0",cluster="group"))

dfdf11 <- broom::tidy(model11)
dfdf11$model <- "purity"
dfdf11$term <- "CDU/CSU"

df_agg <- aggregate(cdu$fairness_care.norm, list(cdu$name, cdu$months, cdu$count, cdu$party), mean)
colnames(df_agg)[1] <- "name"
colnames(df_agg)[2] <- "date"
colnames(df_agg)[3] <- "treat_indirect"
colnames(df_agg)[4] <- "party"
colnames(df_agg)[5] <- "care_harm"

df_agg <- df_agg[order(df_agg$name, -abs(df_agg$treat_indirect) ), ] ### sort first
df_agg <- df_agg %>% distinct(name, date, .keep_all = T)

df_agg$care_harm2 <- scale(df_agg$care_harm)

fixed12 <- plm(care_harm ~ treat_indirect, model="within", index = c("name", "date"), 
               effect = "twoways", data = df_agg)
model12 <- coeftest(fixed12, vcov=vcovHC(fixed12,type="HC0",cluster="group"))

dfdf12 <- broom::tidy(model12)
dfdf12$model <- "care_harm"
dfdf12$term <- "CDU/CSU"




df_rest <- rbind(dfdf1, dfdf2, dfdf3, dfdf4, dfdf5, dfdf6, dfdf7, dfdf8, dfdf9, dfdf10, 
                 dfdf11, dfdf12)

df_rest$model[df_rest$model=="purity"] <- "sanctity/degradation"
df_rest$model[df_rest$model=="care_harm"] <- "care/harm + fairness/injustice"

df_rest$term <- factor(df_rest$term, levels=c("The Left", "Greens", "SPD", "CDU/CSU", "FDP", "AfD"))

dwplot(df_rest, show_intercept = TRUE, ci = .95,
       vline = geom_vline(xintercept = 0, colour = "grey60", linetype = 2)) %>%
  relabel_predictors(c(afd = "AfD", 
                       Greens = "The Greens")) +
  xlab("Coefficient Estimate") + ylab("") +
  scale_colour_manual(name = "Emotion",
                      labels = c("sanctity/degradation", "care/harm + fairness/injustice"),
                      values = c("#6699cc", "#CC6666")) +
  theme_bw()

# Exporting figure to .pdf
ggsave("./figures/figureJ1.pdf")


#### Figure K1 ##################################################################
#load replies to politician tweets
load("./twitter_replies.Rdata")

#merge with twitter df
twitter_replies_merged <- merge(wind_tweets, twitter_replies, by = "id", all.x = T)

# Filter rows from twitter_replies_merged where party is 'AfD' and topic is 46.
# Then, calculate the mean, standard deviation, count, and standard error of the 'replies_disgust' column.
# Also, add a new column 'group' with the value "afd" and a new column 'topic' with the value 46.
afd46 <- twitter_replies_merged %>%
  filter(party == "AfD" & topic == 46) %>%
  summarize(
    mean_disgust = mean(replies_disgust, na.rm = T), 
    sd = sd(replies_disgust, na.rm = T),
    n = n(),
    se = sd / n
  ) %>%
  mutate(group = "afd", topic = 46)

# Similar to the above, but for rows where party is NOT 'AfD' and topic is 46.
# The 'group' column is set to "nonafd".
nonafd46 <- twitter_replies_merged %>%
  filter(party != "AfD" & topic == 46) %>%
  summarize(
    mean_disgust = mean(replies_disgust, na.rm = T), 
    sd = sd(replies_disgust, na.rm = T),
    n = n(),
    se = sd / n
  ) %>%
  mutate(group = "nonafd", topic = 46)

# Combine the results from the above two operations into one dataframe.
combined46 <- rbind(afd46, nonafd46)

# Convert the 'group' column to a factor and set its levels and labels.
combined46$group <- factor(combined46$group, levels = c("afd", "nonafd"),
                           labels = c("AfD replies", "Other Parties' replies"))

# Plot the data from 'combined46'.
# Display the mean level of disgust words in replies for each group (AfD and other parties).
# Also, add error bars to represent the standard error.
combined46 %>%
  ggplot(aes(x = group, y = mean_disgust)) +
  geom_bar(stat="identity", position = position_dodge(), fill = "#CC6666") +
  geom_errorbar(aes(ymin=mean_disgust-se, ymax=mean_disgust+se), width=.2,
                position=position_dodge(.9)) +
  labs(x = "Party", y = "Mean Level of Disgust Words in Replies") +
  theme(text = element_text(size=10), axis.text.x = element_text(angle = 25, vjust = 0.5, hjust=1)) +
  facet_wrap(~ topic) + 
  theme_bw()


# Exporting figure to .pdf
ggsave("./figures/figureK1.pdf")

#### Figure K2 ####################################################################

# Filter rows from twitter_replies_merged where the party is 'AfD' and topic is 46.
# Then, calculate the mean, standard deviation, count, and standard error for the 'replies_disgust' column.
# Finally, add a new column 'topic' with the value "Green Issues".
green_df <- twitter_replies_merged %>%
  filter(party == "AfD" & topic == 46) %>%
  summarize(mean_disgust = mean(replies_disgust, na.rm = T), 
            sd = sd(replies_disgust, na.rm = T),
            n = n(),
            se = sd / n) %>%  
  mutate(topic = "Green Issues")

army_df <- twitter_replies_merged %>%
  filter(party == "AfD" & topic == 3) %>%
  summarize(mean_disgust = mean(replies_disgust, na.rm = T), 
            sd = sd(replies_disgust, na.rm = T),
            n = n(),
            se = sd / n) %>%
  mutate(topic = "Army & Military")

econ_df <- twitter_replies_merged %>%
  filter(party == "AfD" & topic == 38) %>%
  summarize(mean_disgust = mean(replies_disgust, na.rm = T), 
            sd = sd(replies_disgust, na.rm = T),
            n = n(),
            se = sd / n) %>%
  mutate(topic = "Finance & Business")

eu_df <- twitter_replies_merged %>%
  filter(party == "AfD" & topic == 8) %>%
  summarize(mean_disgust = mean(replies_disgust, na.rm = T), 
            sd = sd(replies_disgust, na.rm = T),
            n = n(),
            se = sd / n) %>%
  mutate(topic = "EU")

covid_df <- twitter_replies_merged %>%
  filter(party == "AfD" & topic == 43) %>%
  summarize(mean_disgust = mean(replies_disgust, na.rm = T), 
            sd = sd(replies_disgust, na.rm = T),
            n = n(),
            se = sd / n) %>%
  mutate(topic = "Covid-19 Pandemic")

brexit_df <- twitter_replies_merged %>%
  filter(party == "AfD" & topic == 17) %>%
  summarize(mean_disgust = mean(replies_disgust, na.rm = T), 
            sd = sd(replies_disgust, na.rm = T),
            n = n(),
            se = sd / n) %>%
  mutate(topic = "Brexit")

dig_df <- twitter_replies_merged %>%
  filter(party == "AfD" & topic == 56) %>%
  summarize(mean_disgust = mean(replies_disgust, na.rm = T), 
            sd = sd(replies_disgust, na.rm = T),
            n = n(),
            se = sd / n) %>%
  mutate(topic = "Digitisation")

proc_df <- twitter_replies_merged %>%
  filter(party == "AfD" & topic == 2) %>%
  summarize(mean_disgust = mean(replies_disgust, na.rm = T), 
            sd = sd(replies_disgust, na.rm = T),
            n = n(),
            se = sd / n) %>%
  mutate(topic = "Elections")

# Merge all the dataframes into a single dataframe.
combined_df2 <- rbind(green_df, army_df, econ_df, eu_df, covid_df, brexit_df, dig_df, proc_df)


# Plot the data from 'combined_df2'.
# Display the mean level of disgust words in replies for each topic discussed by the 'AfD' party.
# The topics are ordered in descending order based on the mean_disgust value.
# Error bars are added to represent the standard error.
combined_df2 %>%
  ggplot(aes(x = reorder(topic, -mean_disgust), y = mean_disgust)) +
  geom_bar(stat="identity", position = position_dodge(), fill="#CC6666") +
  geom_errorbar(aes(ymin=mean_disgust-se, ymax=mean_disgust+se), width=.2,
                position=position_dodge(.9)) +
  theme_bw() +
  theme(text = element_text(size=10), axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1)) +
  labs(x = "Topics", y = "Mean Level of Disgust Words in AfD Replies")

# Exporting figure to .pdf
ggsave("./figures/figureK2.pdf")

#### Figure K3 ####################################################################
# Filter tweets from 'spd_tweets' that have mentioned environmental keywords.
spd_climate <- spd_tweets[spd_tweets$environment_count2>0,]

# Similar operations for other datasets.
afd_climate <- afd_tweets[afd_tweets$environment_count2>0,]
sz_climate <- sz_tweets[sz_tweets$environment_count2>0,]
linke_climate <- linke_tweets[linke_tweets$environment_count2>0,]

# Assign a new column 'group' in each dataset to identify the source of the tweets.
spd_climate$group <- "spd"
afd_climate$group <- "afd"
sz_climate$group <- "sz"
linke_climate$group <- "linke"

# Extract only the 'disgust' and 'group' columns from each dataset.
sz_climate2 <- sz_climate[,c("disgust", "group")]
spd_climate2 <- spd_climate[,c("disgust", "group")]
afd_climate2 <- afd_climate[,c("disgust", "group")]
linke_climate2 <- linke_climate[,c("disgust", "group")]

# Combine the above datasets into one.
climate_combined <- rbind(spd_climate2, afd_climate2, sz_climate2, linke_climate2)

# Conduct a one-way analysis of variance (ANOVA) to test if the levels of 'disgust' differ by 'group'.
out1 <- aov(disgust ~ group, data = climate_combined)

# Conduct a Tukey's Honest Significant Difference (HSD) test on the ANOVA results.
# This test is used to find which groups significantly differ from each other.
fit1 <- TukeyHSD(out1, conf.level=.95)

# Convert the results of the TukeyHSD test to a tidy format.
test <- tidy(fit1)

# Filter out the first three rows of the 'test' dataframe.
test <- test[c(1:3),]

# Rename the 'term' column values in the 'test' dataframe.
test$term[1] <- "Left Follow."
test$term[2] <- "SPD Follow."
test$term[3] <- "Pol. Interested"
test$model <- "climate"

# Create a dot-whisker plot of the results using the 'dwplot' function.
# The plot will show the estimated effects (dots) and their confidence intervals (whiskers) for each group.
# A vertical grey line is added at x=0 for reference.
dwplot(test, show_intercept = TRUE, ci = .95,
       vline = geom_vline(xintercept = 0, colour = "grey60", linetype = 2)) +
  ggtitle("Green Issues") +
  theme_bw()


# Exporting figure to .pdf
ggsave("./figures/figureK3.pdf")

#### Figure K4 #################################################################


#Immigration
spd_immigration <- spd_tweets[spd_tweets$immigration_count2>0,]
afd_immigration <- afd_tweets[afd_tweets$immigration_count2>0,]
sz_immigration <- sz_tweets[sz_tweets$immigration_count2>0,]
linke_immigration <- linke_tweets[linke_tweets$immigration_count2>0,]

spd_immigration$group <- "spd"
afd_immigration$group <- "afd"
sz_immigration$group <- "sz"
linke_immigration$group <- "linke"

sz_immigration2 <- sz_immigration[,c("disgust", "group")]
spd_immigration2 <- spd_immigration[,c("disgust", "group")]
afd_immigration2 <- afd_immigration[,c("disgust", "group")]
linke_immigration2 <- linke_immigration[,c("disgust", "group")]

immigration_combined <- rbind(spd_immigration2, afd_immigration2, sz_immigration2, linke_immigration2)
out1 <- aov(disgust ~ group, data = immigration_combined)
fit1 <- TukeyHSD(out1, conf.level=.95)
test <- tidy(fit1)
test <- test[c(1:3),]
test$term[1] <- "Left Follow."
test$term[2] <- "SPD Follow."
test$term[3] <- "Pol. Interested"
test$model <- "Immigration"
immigration_plot <- dwplot(test, show_intercept = TRUE, ci = .95,
                           vline = geom_vline(xintercept = 0, colour = "grey60", linetype = 2)) +
  ggtitle("Immigration") +
  theme_bw() +
  theme(legend.position = "none")


#finance
spd_finance <- spd_tweets[spd_tweets$finance_count2>0,]
afd_finance <- afd_tweets[afd_tweets$finance_count2>0,]
sz_finance <- sz_tweets[sz_tweets$finance_count2>0,]
linke_finance <- linke_tweets[linke_tweets$finance_count2>0,]

spd_finance$group <- "spd"
afd_finance$group <- "afd"
sz_finance$group <- "sz"
linke_finance$group <- "linke"

sz_finance2 <- sz_finance[,c("disgust", "group")]
spd_finance2 <- spd_finance[,c("disgust", "group")]
afd_finance2 <- afd_finance[,c("disgust", "group")]
linke_finance2 <- linke_finance[,c("disgust", "group")]

finance_combined <- rbind(spd_finance2, afd_finance2, sz_finance2, linke_finance2)
out1 <- aov(disgust ~ group, data = finance_combined)
fit1 <- TukeyHSD(out1, conf.level=.95)
test <- tidy(fit1)
test <- test[c(1:3),]
test$term[1] <- "Left Follow."
test$term[2] <- "SPD Follow."
test$term[3] <- "Pol. Interested"
test$model <- "finance"
finance_plot <- dwplot(test, show_intercept = TRUE, ci = .95,
                       vline = geom_vline(xintercept = 0, colour = "grey60", linetype = 2)) +
  ggtitle("Finance + Business") +
  theme_bw() +
  theme(legend.position = "none")




#EU
spd_eu <- spd_tweets[spd_tweets$eu_count2>0,]
afd_eu <- afd_tweets[afd_tweets$eu_count2>0,]
sz_eu <- sz_tweets[sz_tweets$eu_count2>0,]
linke_eu <- linke_tweets[linke_tweets$eu_count2>0,]

spd_eu$group <- "spd"
afd_eu$group <- "afd"
sz_eu$group <- "sz"
linke_eu$group <- "linke"

sz_eu2 <- sz_eu[,c("disgust", "group")]
spd_eu2 <- spd_eu[,c("disgust", "group")]
afd_eu2 <- afd_eu[,c("disgust", "group")]
linke_eu2 <- linke_eu[,c("disgust", "group")]

eu_combined <- rbind(spd_eu2, afd_eu2, sz_eu2, linke_eu2)
out1 <- aov(disgust ~ group, data = eu_combined)
fit1 <- TukeyHSD(out1, conf.level=.95)
test <- tidy(fit1)
test <- test[c(1:3),]
test$term[1] <- "Left Follow."
test$term[2] <- "SPD Follow."
test$term[3] <- "Pol. Interested"
test$model <- "EU"
eu_plot <- dwplot(test, show_intercept = TRUE, ci = .95,
                  vline = geom_vline(xintercept = 0, colour = "grey60", linetype = 2)) +
  ggtitle("Brexit & European Union") +
  theme_bw() +
  theme(legend.position = "none")

#Housing
spd_housing <- spd_tweets[spd_tweets$housing_count2>0,]
afd_housing <- afd_tweets[afd_tweets$housing_count2>0,]
sz_housing <- sz_tweets[sz_tweets$housing_count2>0,]
linke_housing <- linke_tweets[linke_tweets$housing_count2>0,]

spd_housing$group <- "spd"
afd_housing$group <- "afd"
sz_housing$group <- "sz"
linke_housing$group <- "linke"

sz_housing2 <- sz_housing[,c("disgust", "group")]
spd_housing2 <- spd_housing[,c("disgust", "group")]
afd_housing2 <- afd_housing[,c("disgust", "group")]
linke_housing2 <- linke_housing[,c("disgust", "group")]

housing_combined <- rbind(spd_housing2, afd_housing2, sz_housing2, linke_housing2)
out1 <- aov(disgust ~ group, data = housing_combined)
fit1 <- TukeyHSD(out1, conf.level=.95)
test <- tidy(fit1)
test <- test[c(1:3),]
test$term[1] <- "Left Follow."
test$term[2] <- "SPD Follow."
test$term[3] <- "Pol. Interested"
test$model <- "Housing"
housing_plot <- dwplot(test, show_intercept = TRUE, ci = .95,
                       vline = geom_vline(xintercept = 0, colour = "grey60", linetype = 2)) +
  ggtitle("Housing") +
  theme_bw() +
  theme(legend.position = "none")

#military
spd_military <- spd_tweets[spd_tweets$military_count2>0,]
afd_military <- afd_tweets[afd_tweets$military_count2>0,]
sz_military <- sz_tweets[sz_tweets$military_count2>0,]
linke_military <- linke_tweets[linke_tweets$military_count2>0,]

spd_military$group <- "spd"
afd_military$group <- "afd"
sz_military$group <- "sz"
linke_military$group <- "linke"

sz_military2 <- sz_military[,c("disgust", "group")]
spd_military2 <- spd_military[,c("disgust", "group")]
afd_military2 <- afd_military[,c("disgust", "group")]
linke_military2 <- linke_military[,c("disgust", "group")]

military_combined <- rbind(spd_military2, afd_military2, sz_military2, linke_military2)
out1 <- aov(disgust ~ group, data = military_combined)
fit1 <- TukeyHSD(out1, conf.level=.95)
test <- tidy(fit1)
test <- test[c(1:3),]
test$term[1] <- "Left Follow."
test$term[2] <- "SPD Follow."
test$term[3] <- "Pol. Interested"
test$model <- "Military & Army"
military_plot <- dwplot(test, show_intercept = TRUE, ci = .95,
                        vline = geom_vline(xintercept = 0, colour = "grey60", linetype = 2)) +
  ggtitle("Military & Army") +
  theme_bw() +
  theme(legend.position = "none")


#Agriculture
spd_agri <- spd_tweets[spd_tweets$agri_count2>0,]
afd_agri <- afd_tweets[afd_tweets$agri_count2>0,]
sz_agri <- sz_tweets[sz_tweets$agri_count2>0,]
linke_agri <- linke_tweets[linke_tweets$agri_count2>0,]

spd_agri$group <- "spd"
afd_agri$group <- "afd"
sz_agri$group <- "sz"
linke_agri$group <- "linke"

sz_agri2 <- sz_agri[,c("disgust", "group")]
spd_agri2 <- spd_agri[,c("disgust", "group")]
afd_agri2 <- afd_agri[,c("disgust", "group")]
linke_agri2 <- linke_agri[,c("disgust", "group")]

agri_combined <- rbind(spd_agri2, afd_agri2, sz_agri2, linke_agri2)
out1 <- aov(disgust ~ group, data = agri_combined)
fit1 <- TukeyHSD(out1, conf.level=.95)
test <- tidy(fit1)
test <- test[c(1:3),]
test$term[1] <- "Left Follow."
test$term[2] <- "SPD Follow."
test$term[3] <- "Pol. Interested"
test$model <- "Agriculture"
agri_plot <- dwplot(test, show_intercept = TRUE, ci = .95,
                    vline = geom_vline(xintercept = 0, colour = "grey60", linetype = 2)) +
  ggtitle("Agriculture") +
  theme_bw() +
  theme(legend.position = "none")

#Corona
spd_corona <- spd_tweets[spd_tweets$corona_count2>0,]
afd_corona <- afd_tweets[afd_tweets$corona_count2>0,]
sz_corona <- sz_tweets[sz_tweets$corona_count2>0,]
linke_corona <- linke_tweets[linke_tweets$corona_count2>0,]

spd_corona$group <- "spd"
afd_corona$group <- "afd"
sz_corona$group <- "sz"
linke_corona$group <- "linke"

sz_corona2 <- sz_corona[,c("disgust", "group")]
spd_corona2 <- spd_corona[,c("disgust", "group")]
afd_corona2 <- afd_corona[,c("disgust", "group")]
linke_corona2 <- linke_corona[,c("disgust", "group")]

corona_combined <- rbind(spd_corona2, afd_corona2, sz_corona2, linke_corona2)
out1 <- aov(disgust ~ group, data = corona_combined)
fit1 <- TukeyHSD(out1, conf.level=.95)
test <- tidy(fit1)
test <- test[c(1:3),]
test$term[1] <- "Left Follow."
test$term[2] <- "SPD Follow."
test$term[3] <- "Pol. Interested"
test$model <- "Covid-19"
corona_plot <- dwplot(test, show_intercept = TRUE, ci = .95,
                      vline = geom_vline(xintercept = 0, colour = "grey60", linetype = 2)) +
  ggtitle("Covid-19") +
  theme_bw() +
  theme(legend.position = "none")

ggarrange(immigration_plot, finance_plot, eu_plot, housing_plot, military_plot, agri_plot, corona_plot, ncol = 2, nrow = 4)

# Exporting figure to .pdf
ggsave("./figures/figureK4.pdf")
